View Full Version : can't upload image problem

10-16-2002, 09:45 PM
Hi all.
I have tried and tried to fix this but as I'm a newbie I'm having no luck. Got this setup/script made for us - paid a small amount - but am having this one problem.

When I add a product we are supposed to browse for image which it uploads but just the path/image name is stored in database.

The error I get is:
Warning: Unable to create '/var/www/images/uploads/coaster.jpg': No such file or directory in /home/holidays/public_html/admin/addproduct.php on line 316

Warning: Unable to move '/tmp/phpMaCLxK' to '/var/www/images/uploads/coaster.jpg' in /home/holidays/public_html/admin/addproduct.php on line 316
product added successfully

The code for connect.php (which includes the path) is:-

define('UploadDir', $_SERVER['DOCUMENT_ROOT'] . '/images/uploads/' ); // replace /familytravel/images/upload with the path to your upload directory.
define('DBUploadDir', '/images/uploads/'); // replace /familytravel/images/upload with the path to your upload directory. this will be used in URLS
$cnx = @mysql_connect('localhost', 'user', 'password');
if(!$cnx) {
echo('Failed to log into database at this time');

$select = mysql_select_db('holidays_dest');
echo('failed to select database');

function dateSplit($date)
//$splitdate[0] = year
//$splitdate[1] = month
//splitdate[2] = day
$splitdate = explode("-", $date);
return $splitdate;

The line 316 of addproduct.php referred to in the error message is:-

move_uploaded_file($_FILES['files']['tmp_name'], UploadDir . $_FILES['files']['name']);

I have changed the path about a million times and the code for connect.php is what should apparantly work but doesn't.

Thanking you for any help, Sheryll.

10-16-2002, 10:27 PM
Is the directory set up with the right write permissions?

10-17-2002, 08:54 AM
It is set up with 777 which apparantly is correct - although I'm new:))

Thanks, Sheryll.

10-17-2002, 02:18 PM
The first thing you want to do is edit your first post and remove the database access password - not good form to be posting that in a public forum.

Assuming the directory /uploads/ is in the right place (ie in /images/ from public-root) then the script should work. Also assuming your input-file form field is named 'files'.

You could try adding
touch(UploadDir . $_FILES['files']['name']);
prior to the move_uploaded_file line and see if you get a different error return.

10-17-2002, 02:23 PM
One more question...

Is your administration panel running from the same server as the main site?

You have addproduct.php in /home/holidays/public_html/admin
and the images will be in /var/www/images/uploads/
which sort of indicates they are different servers.

10-17-2002, 10:01 PM
Hi Okii and thanks for your replies. Have edited my post as suggested :)

As far as I'm aware the administration panel is running from the same server as the main site but how do I tell? Where on earth does /var/www/ come from? I have never seen this until the error message.

Thanks again, Sheryll.

10-17-2002, 10:10 PM
Hi again.

The input form field is name files.

I added the extra line and tested again - now get this error message:-

Warning: unable to create file /var/www/images/uploads/jamesdean.jpg because No such file or directory in /home/holidays/public_html/admin/addproduct.php on line 316

Warning: Unable to create '/var/www/images/uploads/jamesdean.jpg': No such file or directory in /home/holidays/public_html/admin/addproduct.php on line 317

Warning: Unable to move '/tmp/phpAhZKmv' to '/var/www/images/uploads/jamesdean.jpg' in /home/holidays/public_html/admin/addproduct.php on line 317
product added successfully

Is there anything else I should check?

Thanks, Sheryll.

10-18-2002, 02:23 AM
Hi again.

This newbie is trying to learn by reading all I can find and have found out that /var/www is the "document_root" after running a phpinfo file.

home/holidays/public_html etc is the path to where the html files are.

I'm confused about what I should do next.

Thanks, Sheryll.

10-18-2002, 02:30 AM
Hi again.
Have also learnt from phpinfo file that upload_tmp_dir has "no value". I thought the images were uploaded to a temporary directory so does this mean I have to make one and if so where would I do that?

Thanks, Sheryll.

10-18-2002, 07:41 PM
Right - let's first try to get the script working by pulling out/amending the bits that do not work....

Firstly we need to find the top document that the server uses to parse addproduct.php. All this means is that if you had my_admin.php that then includes addproduct.php , the top document would be my_admin.php. If you actually have www.yourdoamin.com/admin/addproduct.php in the url then addproduct is the top document. Now, suss out which folder that document is in (eg addproduct.php would be in /admin/)

Once you've done that, create a new folder in that folder called 'test' and chmod it to 777 (might need ftp for that).

Now the 'get the script working' bit -
If we use relative linking to define the move_to directory within the script, we can easily target our new folder. This is to ensure the upload to /tmp/ bit is working properly.

move_uploaded_file($_FILES['files']['tmp_name'], UploadDir . $_FILES['files']['name']);

and remove the touch() line now. All that did was try to create a blank file as a test.

If you can save the image using relative paths, you should be able to takes the few steps neccessary to get it into the proper folder.

Good luck Sheryll

10-20-2002, 03:09 AM
Okii - you're brilliant :D

Sorry for delay in replying but I've been away. Anyway I changed the script as per your instructions and guess what - no error messages :)
The images are up there sitting in the test folder. They are not actually showing up on the page when you choose a product from the database but I presume that's cos the path in that part of the script points to the old folder.
A white square box with red cross and no error messages is definately a step in the right direction.

Hope you don't mind telling me what to do next. Thanks again, Sheryll.

10-20-2002, 11:43 PM
Hi Okii.

I'm back :)
Have changed the path in addproduct.php and also in connect.php to /admin/images/uploads/ and now the pic not only uploads it shows up. Very happy here.

As the initial addproduct.php had UploadDir instead of the relative path I presume I now need to learn how to make it work this way. Also was wondering why it is done this way.

Thanks, Sheryll.