Error when trying to upload file in PHP - 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?

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

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

One of two things - either
There is no upload directory
There is no file php1B2.tmp

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

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.

Related

fopen() with error on godaddy

I'm very new here but I'm facing an issue on one of my website with fopen().
$file="//".$_SERVER['SERVER_NAME']."/myfile".date("Ymd").".txt";
if (!file_exists($file)) {
$fp=fopen($file,"w+");
}
$fp=fopen($file,"r+");
And the error message :
fopen(mydomain/myfile20160629.txt): failed to open stream: No such file or
directory in /mydomain/myphpfile.php on line 24
What's funny is that I sometimes get an error message, sometimes not, depending what file use this. And I still got this message when the file is created...
Do you have any idea ?
Try set path : $file="//".$_SERVER['DOCUMENT_ROOT']."/myfile".date("Ymd").".txt";
but before that, move your file in directory $_SERVER['DOCUMENT_ROOT']

move_uploaded_file not working anymore

I made a website a couple of year ago which has worked fine till now.
now, when I try to load an image from the backend it raises me some warning and the Image fails to be moved in the destination directory.
here is the warning I get
Warning: move_uploaded_file(path/image_name.jpg): failed to open
stream: Permission denied in
D:\inetpub\webs\remote_directory\php_script_name.php on line 184
Warning: move_uploaded_file(): Unable to move
'C:\PHPVersions\PHP55\uploadtemp\php418A.tmp' to 'path/image_name.jpg'
in D:\inetpub\webs\remote_directory\php_script_name.php on line 184
here is the line giving me the warning
if (!move_uploaded_file($_FILES['img_'.$pos]['tmp_name'], 'path/'."part_of_image_name".$_FILES['img_'.$pos]['name'])) {
$msg = "<p>Error loading image!!</p>";
break;
}
it does not even print the error message.
I tried to change permissions on directory (and all subdirs and files) via ftp but I'm not sure it succeded because it was giving me the following message
Comando: SITE CHMOD 740 Photo4.jpg.
Risposta: 500 'SITE': command not
understood
(by the way I read somewhere that it does not fix the problem. However the guy said he solved by using some magic which is not working for me - or I'm misunderstanding how to do it)
Everything worked until a few days/weeks ago (the customer wasn't clear on that point) so I just wanted to know if this is a kind of known issue or what (and a possible solution), since after a littlebit of research I could not find anything usefull.
try this, but first create a directory named uploads in the root directory:
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
Found out to solve the problem by using the control panel of site hosting provider to change manually permissions on directory.

Error in imagecreatefromjpeg() function in PHP

When using the below php code it shows an error
<?php
$path="/hari002244/album/f41b6b54811c.jpg";
$src = imagecreatefromjpeg($path);
?>
It shows
SCREAM: Error suppression ignored for
Warning: imagecreatefromjpeg(/hari002244/album/f41b6b54811c.jpg): failed to open stream: No such file or directory in C:\wamp\www\newEmptyPHP.php on line 3
But when using
<?php
$path="f41b6b54811c.jpg";
$src = imagecreatefromjpeg($path);
?>
It doesn't show any error.
I had typed the path in URL bar and checked whether file exists.It is working perfectly.
Can you tell me why this error occurs? and also how to overcome this?
Thanks in advance.
The first path, "/hari002244/album/f41b6b54811c.jpg" is an absolute path, it starts from the root of the whole file system. Chances are, this path does not exist.
In the second case, it is just a filename "f41b6b54811c.jpg" which is relative to / located in the current working directory, which of course exists.
Got the answer
Instead of "/hari002244/album/f41b6b54811c.jpg"
Should be used "http://localhost/hari002244/album/f41b6b54811c.jpg"

PHP: Uploading troubles

I'm making a image uploader but I have come across 2 errors. Which are:
Warning: move_uploaded_file(upload/Corgi 007.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/stationr/public_html/admin/doupload.php on line 12
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/chroot/tmp/phpMvRSbS' to 'upload/Corgi 007.jpg' in /home/stationr/public_html/admin/doupload.php on line 12
The errors are occuring when I call the move_uploaded_file() method. Here is my code (note this has no error checking what so ever it is just bare ones uploading).
<?php
error_reporting(E_ALL);
error_reporting(-1);
ini_set('error_reporting', E_ALL);
$filename = $_FILES["file"]["name"];
$filesize = $_FILES["file"]["size"];
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/$filename");
?>
You've failed to check if the upload succeeded at all:
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
move file ...
} else {
die("Upload failed with error code " . $_FILES['file']['error']);
}
The error codes are documented here: http://www.php.net/manual/en/features.file-upload.errors.php
As well, is there a "upload" subdirectory in whatever your script's current working directory is? Does the web server process have write permissions on that directory?
And as a MAJOR security hole, blindlly using the ['name'] parameter of the upload as your target filename allows a malicious user to scribble on ANY file on your server. Never ever blindly use that filename. If the upload filename is provided as (say) ../../../../../../etc/passwd, you've now allowed someone to replace your system password file.

thumbs.db messing up my upload routine

I'm getting the following error while uploading a zip archive.
Warning: ZipArchive::extractTo(C:\xampplite\htdocs\testsite/wp-content/themes/mytheme//styles\mytheme/Thumbs.db) [ziparchive.extractto]: failed to open stream: Permission denied in C:\xampplite\htdocs\testsite\wp-content\themes\mythem\uploader.php on line 17
The thing I can't quite figure is that I don't see a thumbs.db file in either the zip archive or the destination folder that was created (the upload still processes, I just get these errors).
The function is below, line 17 is commented...
function openZip($file_to_open) {
global $target;
$zip = new ZipArchive();
$x = $zip->open($file_to_open);
if($x === true) {
$zip->extractTo($target); //this is line 17
$zip->close();
unlink($file_to_open);
} else {
die("There was a problem. Please try again!");
}
}
Looks like just a warning. I would ignore it. I'm betting the error happens when it tried to overwrite the thumbs.db file in the extraction directory.
Alternatively, use getNameIndex to get a list of files, filter thumbs.db and use the second parameter of extractTo to filter the files.
http://www.php.net/manual/en/function.ziparchive-getnameindex.php
http://www.php.net/manual/en/function.ziparchive-extractto.php
This path is all messed up
C:\xampplite\htdocs\testsite/wp-content/themes/mytheme//styles\mytheme/Thumbs.db
I guess this might be windows issue.
can you add a line above line 17 saying
echo $file_to_open; exit;
and tell us what it says when you try to upload file?
Assuming that the mangled path works, then there is a thumbs.db in the destination directory (by default a hidden file), and/or your script does not have permission to write to that file/directory. Windows is very big on spitting out "permission denied" when a file is in use by another process, rather than saying "In use by process XXX".

Categories