Yogi Yang 007
Yogi Yang 007

Reputation: 5251

Error when trying to upload file in PHP

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?

Upvotes: 2

Views: 6731

Answers (6)

Yogi Yang 007
Yogi Yang 007

Reputation: 5251

@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.

Upvotes: 1

James
James

Reputation: 82136

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.

Upvotes: 0

bobobobo
bobobobo

Reputation: 67296

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

Upvotes: 0

Grouchal
Grouchal

Reputation: 9796

One of two things - either

  1. There is no upload directory
  2. There is no file php1B2.tmp

Upvotes: 0

phihag
phihag

Reputation: 288220

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.

Upvotes: 2

robjmills
robjmills

Reputation: 18598

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

Upvotes: 0

Related Questions