Jump to content
Larry Ullman's Book Forums

Move_Uploaded_File Function


Recommended Posts

Hi all,

I am getting the following error message when trying to move an uploaded file:

 

 

An error has occurred in script 'C:\xampp\htdocs\Textures\web\admin_snippets_new.php' on line 64: move_uploaded_file(../uploads/green.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory

Date/Time: 11-9-2012 16:07:22

. print_r(Array, 1) .

 

An error has occurred in script 'C:\xampp\htdocs\Textures\web\admin_snippets_new.php' on line 64: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\php8BD9.tmp' to '../uploads/green.jpg'

Date/Time: 11-9-2012 16:07:22

. print_r(Array, 1)

 

Here's the code in question:

 

if (isset($_FILES['upload_snippet']))

{

//Check to see if there are any upload errors

if (!empty($_FILES['upload_snippet']['error']))

{

if ($_FILES['upload_snippet']['error'] = 4)

{

echo'

<p class="error_msg">There is a problem with the file upload, there has been no file chosen.</p>

';

}

else

{

echo'

<p class="error_msg">There is a problem with the file upload, talk to Paul.</p>

';

}

 

 

}

else

{

//The file should be a JPG

$file_allowed = array('image/JPG', 'image/pjpeg', 'image/jpeg');

 

if (in_array($_FILES['upload_snippet']['type'], $file_allowed))

{

//Move the file from it's temporary location to the uploads directory

move_uploaded_file($_FILES['upload_snippet']['tmp_name'], "../uploads/{$_FILES['upload_snippet']['name']}");

 

}

else

{

echo'

<p class="error_msg">Only JPG files are allowed.</p>

';

}

}

}

echo print_r($_FILES);

 

The above array that I've echoed is:

 

Array ( [upload_snippet] => Array ( [name] => green.jpg [type] => image/jpeg [tmp_name] => C:\xampp\tmp\php8BD9.tmp [error] => 0 => 3263 ) ) 1

 

As you can see there are no errors in the file upload. The uploads directory is one level up from the coded file, in the same place as the mysql connect script, so outside the web directory. I've checked the folder itself in Windows 7, right clicked and checked that the security permissions are all set to 'allow'. The directory name is the same as the code above, no typo's that I can see.

 

Not sure where else to check. Any ideas would be appreciated.

 

Cheers

Paul

Link to comment
Share on other sites

Hi Larry,

Path of the uploads directory:

C:\Users\Paul\Documents\Work\Projects\Work in Progress\Textures\code\uploads

 

Path of the script:

C:\Users\Paul\Documents\Work\Projects\Work in Progress\Textures\code\web\admin_snippets_new.php

 

Cheers

Paul

Link to comment
Share on other sites

Hi all,

An update. I changed the code this morning and tried the following:

 

$file_allowed = array('image/JPG', 'image/pjpeg', 'image/jpeg');

 

if (in_array($_FILES['upload_snippet']['type'], $file_allowed))

{

//Generate a temporary name for the uploaded image that will be changed to the snippet name latet

$temp_name = '../uploaded_snippets/' . md5($_FILES['upload_snippet']['name']);

 

//Move the file from it's temporary location to the uploads directory

if (move_uploaded_file($_FILES['upload_snippet']['tmp_name'], $temp_name))

{

echo'

<p>It worked</p>';

}

else

{

echo'

<p>It didn\'t worked</p>';

}

}

else

 

It didn't work.

 

I then changed it to an absolute path, as follows:

 

//The file should be a JPG

$file_allowed = array('image/JPG', 'image/pjpeg', 'image/jpeg');

 

if (in_array($_FILES['upload_snippet']['type'], $file_allowed))

{

//Generate a temporary name for the uploaded image that will be changed to the snippet name latet

$temp_name = 'C:\Users\Paul\Documents\Work\Projects\Work in Progress\Textures\code\uploaded_snippets\\' . md5($_FILES['upload_snippet']['name']);

 

//Move the file from it's temporary location to the uploads directory

if (move_uploaded_file($_FILES['upload_snippet']['tmp_name'], $temp_name))

{

echo'

<p>It worked</p>';

}

else

{

echo'

<p>It didn\'t worked</p>';

}

}

else

 

And this worked.

 

Unfortunately I have no idea why. If you compare the absolute paths as per my last post I don't understand why the first bit above doesn't work.

 

Cheers

Paul

Link to comment
Share on other sites

Hi all,

I've sussed it.

 

I use Dreamweaver and localhost. When a file is to be previewed I press F12, it saves a copy to localhost and displays it in Firefox. I had created the uploads directory on my C drive (hence the absolute path worked) but not on localhost (hence the relative path didn't). So in localhost the directory didn't exist.

 

Cheers

Paul

Link to comment
Share on other sites

 Share

×
×
  • Create New...