views:

582

answers:

6

I am php page everything was working fine till today morning. Now the page is not uploading any selected file. All that I keep getting is the following error message:

Warning: move_uploaded_file(upload/BrainStream_2009_06_25_23041.zip) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\xampp\htdocs\vectorization\admin\jobs_edit.php on line 146

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\Documents and Settings\Admin\Local Settings\Temp\php1B2.tmp' to 'upload/BrainStream_2009_06_25_23041.zip' in C:\xampp\htdocs\vectorization\admin\jobs_edit.php on line 146 File could not be uploaded. Please select a valid file. File Name:BrainStream.zip

I have written following code:

$uplfile = $_FILES['uploadfile']['name'];   
$upltmp = $_FILES["uploadfile"]["tmp_name"];
if(!empty($uplfile))
{  
 $ext = explode(".", $uplfile);

 $upload_date = date("Y_m_d"); //use this variable to change file name to avoid conflict with same name files
 $upload_dir = "upload/";

 $file_name=$ext[0]."_".$upload_date."_".rand(0, getrandmax()).".".$ext[1];

 (move_uploaded_file($upltmp,$upload_dir.$file_name))
 }

I have XAMPP stack installed on my PC which is running WinXP, has 3 GB RAM and ample of Hard disk space.

No matter which size file I select it always give error.

What must be wrong in this code?

A: 

looks like your problem could be that you're using forward slashes for your upload directory but on windows this would be a backslash, you also need to make sure that the upload directory is relative to the script. if not provide a full path.

A good tip to prevent the slashes issue is to use the DIRECTORY_SEPARATOR constant

seengee
Windows' directory separators are \ and /, so that's not a problem. Generally, use DIRECTORY_SEPARATOR in php or just /.
phihag
oh really, i never work on windows but had always kinda assumed the reason for DIRECTORY_SEPARATOR was to prevent issues like this. If not, then whats its purpose?
seengee
+1  A: 

Interesting syntax in the last line. The error indicates the problem is in that line and either the source file or destination directory is missing. Since the first one is automatically generated, make sure that C:\xampp\htdocs\vectorization\admin\upload exists and is writable.

phihag
A: 

One of two things - either

  1. There is no upload directory
  2. There is no file php1B2.tmp
Grouchal
A: 

This may seem obvious, but be sure to double check your php.ini

file_uploads = On
upload_tmp_dir = "C:\xampp\tmp"
upload_max_filesize = 64M
bobobobo
A: 

What might be useful for you to do is a quick check to ensure the file has been uploaded successfully e.g.

switch ($_FILES["cv"]["error"])
{
    case UPLOAD_ERR_FORM_SIZE:
       // handle error
    case UPLOAD_ERR_INI_SIZE:
       // handle error
    case UPLOAD_ERR_PARTIAL:
       // handle error
    case UPLOAD_ERR_NO_FILE:
       // handle error
    case UPLOAD_ERR_CANT_WRITE:
       // handle error
}

Its a better way of handling the errors you may encounter when uploading files.

James
A: 

@phihag,

Thanks for the hint. One of the new developer while studying the source had by mistake remove (../) in the $upload_dir variable assignment.

$upload_dir = "upload/"; //this is wrong

Actually it was set as

$upload_dir = "../upload/"; //this works but accidentally edited by another developer

What a lamer I am. I could not spot the problem.

Anyways thanks once one for your help to solve my problem.

Yogi Yang 007