move_uploaded_file not working anymore - php

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.

Related

PHP/IIS copy()/move_uploaded_file() failed to open stream: Permission denied Warning

I am relatively new to web development and currently ran into an error I can't solve.
I use IIS to test my php after typing in "http://localhost/index.html" and select the example file to upload, it generates the following warnings:
Warning: move_uploaded_file(upload/Angela_Nie_resume_technical - EditedMW.doc): failed to open stream: Permission denied in C:\inetpub\wwwroot\test.php on line 26
Warning: move_uploaded_file(): Unable to move 'C:\Windows\Temp\php74F2.tmp' to 'upload/Angela_Nie_resume_technical - EditedMW.doc' in C:\inetpub\wwwroot\test.php on line 26
Below is the code that is related with moving my files.
$target_dir = "upload/";
$target_file = $target_dir . basename( $_FILES["uploaded"]["name"]) ;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target_file)) {
echo "The file has been uploaded";
}
else {
echo "Sorry, there was a problem uploading file.";
}
I am using Windows 8.1 and using IIS to run localhost. Thank you in advance for helping me out!!
In my situations, the IUSR account needed permissions in the destination directory. Not the ISS_IUSR account, just the IUSR account.
That means that user who is running the apache server does't have write permissions for upload/ directory. You can right click the folder and set permission for writing to everyone.
Update
It might be because your PHP is in safe mode and doesn't execute commands like move one file to another. Check your php.ini for safe_mode and safe_mode_exec_dir and experiment with them.

PHP move_uploaded_file getting errors and file is not being uploaded

I have a form and with input type file....what I am trying to do is save that file to the server (in this case, localhost)
move_uploaded_file($_FILES["image"]["tmp_name"], "/admin-uploads/" . $_FILES["image"]["name"]);
and this is the error I get:
Warning: move_uploaded_file(/admin-uploads/home.jpg): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/Site/admin/Insert.php on line 30
Warning: move_uploaded_file(): Unable to move '/Applications/XAMPP/xamppfiles/temp/phpsoF2wg' to '/admin-uploads/home.jpg' in /Applications/XAMPP/xamppfiles/htdocs/Site/admin/Insert.php on line 30
Am I missing code?
Assuming "/admin-uploads/" directory is a subdirectory of where this script is run. You need the full path to the destination directory.
as:
move_uploaded_file($_FILES["image"]["tmp_name"], __DIR__."/admin-uploads/" . $_FILES["image"]["name"]);
fyi: please be aware that the 'user supplied' [image][name] may not be a valid filename. it may be worthwhile 'sanitizing' it.

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

php function move_uploaded_file wont work on my online server but does on localhost

I have been working on a upload file that works fine on localhost (windows) but wont work on my linux server I rent. I want it to move it to a subdir '/files' from where the file is.
it gives the errors:
Warning: move_uploaded_file(/home/taalhulp/domains/taalhulpmanager.nl/public_html/files/6) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/taalhulp/domains/taalhulpmanager.nl/public_html/Gegev_Dossier.php on line 66
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpFMaeAs' to '/home/taalhulp/domains/taalhulpmanager.nl/public_html/files/6' in /home/taalhulp/domains/taalhulpmanager.nl/public_html/Gegev_Dossier.php on line 66
Here is a simplified version of my code
$FotoMap = '/home/taalhulp/domains/taalhulpmanager.nl/public_html/files/' ;
if ($_POST['type'] == 'add' & strlen($_FILES['FileUpload']['name']) > 0 && strlen($_POST["GivenFileName"]) > 0 )
{
if (move_uploaded_file($_FILES['FileUpload']['tmp_name'], $FotoMap . $file_name_id))
{
$_SESSION['echo'] = 'succes';
}
else
{
$_SESSION['echo'] = 'fail';
}
}
it says permission denied but is that because I do something wrong or because i need to contact my provider to tell him to enable that, maybe you I can enable that myself?
Make sure that /home/taalhulp/domains/taalhulpmanager.nl/public_html/files/'s permissions are set to 0777.
I do not recommend doing that as it opens a bunch of security issues. You would be better finding out on which user or group the script is being run and modify the directory to be owned by that user instead.
you probably don't have permissions (PHP engine / apache webserver) to access the directory for temporary uploads. try to set your custom temp path.

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

Categories