Image Uploading Not Working

I am having trouble with uploading an Image to my server files.
I am building an online store where, when i add a product, a can select an image for it to.
I have checked my php.ini file to allow uploads, as well as sufficient size of file allowed.
I have set the permissions to the folder where the images are supposed to go to “full control” for “Everyone”
(just to avoid any problems there)

Now, when i open my add product page there is a “Upload Image” button next to a text field,
that opens a separate window containing the upload page code.
The text field is to link the image on my database.

So I click on the “Upload Image” button and the following happens:
Small separate window appears with 2 buttons, Browse and Upload.
I click on browse, and select my image.
The image name now displays next to the browse button.
When I click Upload, it closes the page, the image name displays in the text field next to the “Upload Image”
button in the product adding list for database reference.
I click on add product, and the product , including the image name displays on the database.

But the image did not upload to the specified server folder.

Here is my code…

<body>	
<?php if ((isset($_POST["send"])) && ($_POST["send"] == "form1")) {
	$file_Name = $_FILES['userfile']['name'];
	move_uploaded_file($_FILES['userfile']['tmp_name'], "../graphics/products/".$file_Name);
	?>
	<script>
		opener.document.form1.strImage.value="<?php echo $file_Name; ?>";
		self.close();
	</script>
<?php
}
else
{?>
<form action="imageupload.php" method="post" enctype="multipart/form-data" id="form1" />
<p>
	<input name="userfile" type="file">
</p>
<p>
	<input type="submit" name="button" id="button" value="Upload Image" />
</p>
	<input type="hidden" name="send" value="form1" />
</form>
<?php }?>
</body>

Well, C-Gut, it is most likely not pointing to the folder correctly. How have you debugged this so far?

My guess is that it is a folder location error. You have it as: “…/graphics/products/”.$file_Name

This means you back up one folder from the current location of the PHP code page and then move up two
folders. If you are calling this routine from another folder, it might be showing the wrong location for the
file to be moved to. If your “/graphics/products/” folder is in the root of your server, then remove the “…”
to store it there. Depends on your server folder layout.

Not sure if that helps, but, that is my guess…

I have tried inserting the entire file path…

“D:/Web/shop/graphics/products”
Didn’t work.

(it is in D:/Web… because I have multiple small drives running different servers.)

I am confused! You have a server and it is online. You have web code ( PHP ) which ONLY runs on your 1
server. So, the file you upload to that one server can NOT be a local “D:” drive. It must be a folder on the
same server your user submit’s it to. It is hard to cross-server a file using a posted submit code.
BUT, you can copy it with easy after it is uploaded to server #1. Just copy it to the other server like this:
( not tested… )
[php]
// Source File URL
$remote_file_url = ‘http://origin-server-url/filename.xyz’;

// New file name and path for this file
$local_file = ‘files.zip’;

// Copy the file from source url to server
$copy = copy( $remote_file_url, $local_file );

// Add notice for success/failure
if( !$copy ) {
echo “Failed to copy $file…\n”;
} else {
echo “Success to copy $file…\n”;
}
[/php]
This would copy from one server to the other as long as the permissions allow everyone to copy files…

You would basically, place this right after the move_uploaded_file() function. (After moving it to a server#1
uploads folder…) Hope that helps…

Another good way is to use CURL. Basically, you still upload the file on server#1 and move it to that server’s
uploads folder.

Then, you run a CURL function to send the file to the other server as a posted page. On the second server,
you will have a handler PHP script to grab the incoming file and store it there. This is a fairly common way to
do this process. It also makes it a bit secure as you can add in security checks if you wish. I looked around
the web for you and found this explanation. I think you can follow it…

http://subinsb.com/upload-image-to-remote-server-with-php-curl-and-handling-file-in-remote-server

And, of course, you could use FTP and just send it to the second server that way. FTP in PHP is not very
hard to set up. Hope these ideas help…

Hey.
Thank you so much for the suggestions and quick responses.

ErnieAlex…

I have one physical PC with multiple small drives.
Only one drive has my web server files.
The others have Minecraft and TeamSpeak.

The PC is in a room next to my office at home on a local network.
I could upload the images manually with file browser or FTP but,
In the near future there will not only be me uploading and adding products to the store.

That’s why I need this to work.

Meanwhile I will try your suggestions…

Well, I did a lot of reading on PHP and file copies. It should work with just a standard copy function.
I have not tested it. But, if your one computer has all of the drives attached, the PHP system should be
able to copy them with just a standard command…

The only catch is that you do not use the move function to store it. Just move that file to your server’s
uploads folder, then, move or copy it to the needed folder on the other drive. This is code to copy a file
using the local drives. You can also use MOVE instead of copy. BUT, the file actually has to exist. This does
mean that you first have to move_uploaded_file to a web folder on the server, then do the move to local
drive…

$srcfile=‘C:\File\Whatever\Path\somefilename.txt’;
$dstfile=‘G:\Shared\Reports\somefilename.txt’;
copy($srcfile, $dstfile);

As far as I read online, this is how simple it is. Try it and let us know…

Sorry to sound stupid…

Where in my original code would i put this?

Well, in your original code, which had this line:
move_uploaded_file($_FILES[‘userfile’][‘tmp_name’], “…/graphics/products/”.$file_Name);
You would have to make sure that the temporary file is stored in a folder on that server first.
So, the last part of that line would have to point to the correct folder on the server that the page
is on. Did you get that part working yet?

Then, right after that line, you would copy the file from one server disk drive to the other one that is not
attached to the server. Then, once copied, you can delete it from the original folder.

Hope that helps…

move_uploaded_file($_FILES['userfile']['tmp_name'], "../graphics/products/".$file_Name);

Is pointing to the folder where the sight is, where I want my images to be.

Do i need to change it to a temp folder in the sight folder, then copy it from the temp folder to the products folder that I want?

This is what I did and it gave me a internal server error.

<?php if ((isset($_POST["send"])) && ($_POST["send"] == "form1")) {
$file_Name = $_FILES['userfile']['name'];
move_uploaded_file($_FILES['userfile']['tmp_name'], "../../temp/".$file_Name);
$srcfile='../../temp/';
$dstfile='../shop_images/products';
copy($srcfile, $dstfile);
?>
<script>
	opener.document.form1.strImage.value="<?php echo $file_Name; ?>";
	self.close();
</script>
<?php
}
else
{?>
<form action="add_image.php" method="post" enctype="multipart/form-data" id="form1" />
<p>
<input name="userfile" type="file">
</p>
<p>
<input type="submit" name="button" id="button" value="Upload Image" />
</p>
<input type="hidden" name="send" value="form1" />
</form>
<?php }?>

C-GuL, I am confused what you want. You have a website. It uploads images to the server’s folder.
You move the file to that folder using the move_uploaded_file() function. Then, you want to move it to
another folder not on the server’s folder system, correct? But, in the code you show, the source folder and
the destination folder are both on the server. So, why do that?

When you upload a file to a server it goes to the temporary folder on the server.
Next, you use the move_uploaded_file() function to move it to the folder you really want it in.

In the new code you just posted, it then moves it to a second folder on the same server.
Is that what you want to do? In your first post, you said the file was not uploading correctly which means
that your destination folder in the move_uploaded_file() function was not correct. Then another post said
you wanted to upload the file and them move it off to a secondary drive not on the server. (“D:/”)

Let’s just get the upload working first. You need to point the move_uploaded_file() function to a valid
folder on your server. Do you have that section working?

After some thought. I think what you are wanting is a shared folder. On Linux it would be a symbolic link to another directory.

For windows:
Right click on the folder
Share With
Advanced Sharing
Share the directory.

In the original upload directory:
create a shortcut
Link the shortcut to the shared directory.

Now, when a file is stored in that dirctory, it should be in the other as well.

ErnieAlex…

The image is not supposed to go to a secondary folder.
That was a post from you.

'When you upload a file to a server it goes to the temporary folder on the server.
Next, you use the move_uploaded_file() function to move it to the folder you really want it in.'

This is what was supposed to happen in the code from the first post.

The latter code I posted is with the suggestion you made.

'$srcfile='C:\File\Whatever\Path\somefilename.txt';
$dstfile='G:\Shared\Reports\somefilename.txt';
copy($srcfile, $dstfile);'

All i want is code for a pag to upload an image to my products folder…

I have tried inserting the entire file path...

“D:/Web/shop/graphics/products”
Didn’t work.

(it is in D:/Web… because I have multiple small drives running different servers.)


You mentioned hard-coded drives which usually means, non-server drives. Sorry I did not understand!

Now, let’s say your site’s folders are all in the root of the site. So, you have code in a page in the root.
You also have a folder named “product_images” in the root. The following page, as-is should be able
to allow a user to load an image and place it into that folder. There is no error checking or security to make
sure that the user is not sending you a program file or something incorrect, but, it should work as-is. Most
of this was from your post.
[php]

<?php if (isset($_POST["send"])) { $file_Name = $_FILES['userfile']['name']; move_uploaded_file($_FILES['userfile']['tmp_name'], "product_images/" . $file_Name); } ?> Load a file test... [/php] Note: This page posts to itself and therefore does not show anything on the page. It is just the simplest example of uploading a file to a folder. To test it, move it to your server, go to the page, select an image to upload and submit it. Look on the server under the folder and see if the file was moved to the correct folder as it should be. Of course, this points to a folder in the root of the server. You would have to alter that to where you want it really stored.

I would alter the simple code to catch exceptions. The last time I did one of these I added things like, is_writable to check if everything was able to take the file. And throw exceptions if something went wrong.

Yes, Astonecipher, I agree… Perhaps you can post some sample code for him based on the simple code I
posted for him. I think I was confused in what he was asking. He already made the folder full permissions!

This does a lot more than you were looking for, but this is what I was getting at. If needed I could attempt to upload the uploads module this resides in.

[php]
try {

// if user is a name, get the users id for storage
if (! filter_var ( $_POST [‘owner’], FILTER_VALIDATE_INT )) {
$user_info = new _config_users ();
$id = $user_info->getUserIdByUserName ( $_POST [‘owner’] );
$_FILES [‘file_upload’] [‘owner’] = $id [0] [‘user_id’];
} else {
$_FILES [‘file_upload’] [‘owner’] = $_POST [‘owner’];
}
if ( !filter_var ( $_POST[‘expire’], FILTER_VALIDATE_INT))
$_FILES [‘file_upload’] [‘exp’] = 30;
else
$_FILES [‘file_upload’] [‘exp’] = (int) $_POST[‘expire’];

if ( is_dir( $_POST[‘location’] ) ) {
$_FILES [‘file_upload’] [‘location’] = $_POST[‘location’] . “/”;
// $target_dir = $_POST[‘location’] . “/”;
} else
throw new Exception( ‘File could not be stored in "’ . htmlentities( $_POST[‘location’] ) . ‘".’);
$_FILES [‘file_upload’] [‘desc’] = $_FILES [‘file_upload’] [‘name’];

$f = new File ( $_FILES );
$link = new uploads_hd_ticket ();

$f->begin ();

// echo “

”;
// print_r ( $_FILES );
// echo “
”;
if (is_writable ( ‘/tmp’ ) && is_writable ( $target_dir )) {
if (move_uploaded_file ( $_FILES [‘file_upload’] [‘tmp_name’], $target_dir . $f->getAssignedName() ) ) {
// echo “File Moved”;
  $f->insertFile ();
  
  $id = $f->last_insert_id ();

// echo “

”;
// print_r ( $id );
// echo “
”;
$params [‘file_id’] = $id;
$params [‘ticket_id’] = $_POST [‘ticket_id’];
$link->insertData ( $params );
  $f->commit ();
} else
  echo "There was an issue uploading your file.";

} else {

$t = is_writable( $target_dir ) ? 'true' : 'false';
$fi = is_writable( $_FILES ['file_upload'] ['tmp_name'] ) ? 'true' : 'false';
$out = "\n" . $target_dir . " is writable: " . $t . "\n\r";
$out .= $_FILES ['file_upload'] ['tmp_name'] . " is writable: " . $fi . "\n\r";
throw new Exception( $out );

}

} catch ( Exception $e ) {

if ( $f )
$f->rollback ();
echo “{$e->getMessage()}”;
$err = $e->getMessage ();
$err .= " Received in " . $e->getFile() . “\n\r” . $e->getTraceAsString();
error_log( $err ) ;

}
[/php]

Sorry I was gone for a couple of days…

I ran this code as “astonecipher” sugested, in a separate page…

[code]

<?php $file = "../shop_images/products"; if(is_writable($file)) { echo ("$file is writeable"); } else { echo ("$file is not writeable"); } ?> [/code]

And it returned…

    ../shop_images/products is writeable

So is it safe to say the problem doesn’t lie with the folder permissions?

Then I ran the “Upload” code that “ErnieAlex” sugested…

[code]



<?php
if (isset($_POST[“send”])) {
$file_Name = $_FILES[‘userfile’][‘name’];
move_uploaded_file($_FILES[‘userfile’][‘tmp_name’], “…/shop_images/products” . $file_Name);
}
?>

[/code]

When I press the “Upload Image” button the page flashes, I close it, and check the folder.
Nothing in it again.

I have not tried what “astonecipher” posted yet.

LINK TO THE PAGE:
http://www.compumodsa.com/shop/admin/add_image.php

If you can check from your side?

I am seriously struggling here with only base web development knowledge, and I can’t continue my project til i get this freakin upload to work.

Well, just one more fix and you will have it working… Replace this line:
[php]
move_uploaded_file($_FILES[‘userfile’][‘tmp_name’], “…/shop_images/products” . $file_Name);
[/php]

With this one… Note that you did not tell it a folder name, but, a filename “products-somefile-name…”
[php]
move_uploaded_file($_FILES[‘userfile’][‘tmp_name’], “…/shop_images/products/” . $file_Name);
[/php]
If this was my fault, I am sorry… You need to end the folder with a slash and then the filename…
Hope that solves it for you!

I have considered it and tried it but it still doesn’t work.
I keep thinking it is a server side problem as none of the coding thus far has worked.
But up and downing on the server i can’t for the life of me figure out what the problem is.

I have added…

error_reporting(E_ALL); ini_set('display_errors', 1);

Inside my php tag to check, but no errors are displayed.

Sponsor our Newsletter | Privacy Policy | Terms of Service