PHP Error - Uploading a file - php

I'm trying to write some PHP to upload a file to a folder on my webserver. Here's what I have:
<?php
if ( !empty($_FILES['file']['tmp_name']) ) {
move_uploaded_file($_FILES['file']['tmp_name'], './' . $_FILES['file']['name']);
header('Location: http://www.mywebsite.com/dump/');
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Dump Upload</title>
</head>
<body>
<h1>Upload a File</h1>
<form action="upload.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000000" />
Select the File:<br /><input type="file" name="file" /><br />
<input type="submit" value="Upload" />
</form>
</body>
</html>
I'm getting these errors:
Warning: move_uploaded_file(./test.txt) [function.move-uploaded-file]: failed to open stream: Permission denied in E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php on line 3
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\Temp\phpA30E.tmp' to './test.txt' in E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php on line 3
Warning: Cannot modify header information - headers already sent by (output started at E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php:3) in E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php on line 4
PHP version 4.4.7
Running IIS on a Windows box. This particular file/folder has 777 permissions.
Any ideas?

OMG
move_uploaded_file($_FILES['file']['tmp_name'], './' . $_FILES['file']['name']);
Don't do that. $_FILES['file']['name'] could be ../../../../boot.ini or any number of bad things. You should never trust this name. You should rename the file something else and associate the original name with your random name. At a minimum use basename($_FILES['file']['name']).

As it's Windows, there is no real 777. If you're using chmod, check the Windows-related comments.
Check that the IIS Account can access (read, write, modify) these two folders:
E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\
C:\WINDOWS\Temp\

Try adding a path. The following code works for me:
<?php
if ( !empty($_FILES['file']) ) {
$from = $_FILES['file']['tmp_name'];
$to = dirname(__FILE__).'/'.$_FILES['file']['name'];
if( move_uploaded_file($from, $to) ){
echo 'Success';
} else {
echo 'Failure';
}
header('Location: http://www.mywebsite.com/dump/');
exit;
}
?>

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\Temp\phpA30E.tmp' to './people.xml' in E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php on line 3
is the important line it says you can't put the file where you want it and this normally means a permissions problem
check the process running the app (normally the webservers process for php) has the rights to write a file there.
EDIT:
hang on a bit
I jumped the gun a little is the path to the file in the first line correct?

Another think to observe is your directory separator, you are using / in a Windows box..

Add the IIS user in the 'dump' folders security persmissions group, and give it read/write access.

Create a folder named "image" with folder permission 777
<?php
move_uploaded_file($_FILES['file']['tmp_name'],"image/".$_FILES['file']['name']);
?>

We found using below path
{['DOCUMENT_ROOT'] + 'path to folder'
and giving everyone full access to the folder resolved the issue.
Make sure to not reveal the location in the address bar. No sense in giving the location away.

Related

failed to open stream: Permission denied for php move upload file [duplicate]

I'm trying to write some PHP to upload a file to a folder on my webserver. Here's what I have:
<?php
if ( !empty($_FILES['file']['tmp_name']) ) {
move_uploaded_file($_FILES['file']['tmp_name'], './' . $_FILES['file']['name']);
header('Location: http://www.mywebsite.com/dump/');
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Dump Upload</title>
</head>
<body>
<h1>Upload a File</h1>
<form action="upload.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000000" />
Select the File:<br /><input type="file" name="file" /><br />
<input type="submit" value="Upload" />
</form>
</body>
</html>
I'm getting these errors:
Warning: move_uploaded_file(./test.txt) [function.move-uploaded-file]: failed to open stream: Permission denied in E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php on line 3
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\Temp\phpA30E.tmp' to './test.txt' in E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php on line 3
Warning: Cannot modify header information - headers already sent by (output started at E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php:3) in E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php on line 4
PHP version 4.4.7
Running IIS on a Windows box. This particular file/folder has 777 permissions.
Any ideas?
OMG
move_uploaded_file($_FILES['file']['tmp_name'], './' . $_FILES['file']['name']);
Don't do that. $_FILES['file']['name'] could be ../../../../boot.ini or any number of bad things. You should never trust this name. You should rename the file something else and associate the original name with your random name. At a minimum use basename($_FILES['file']['name']).
As it's Windows, there is no real 777. If you're using chmod, check the Windows-related comments.
Check that the IIS Account can access (read, write, modify) these two folders:
E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\
C:\WINDOWS\Temp\
Try adding a path. The following code works for me:
<?php
if ( !empty($_FILES['file']) ) {
$from = $_FILES['file']['tmp_name'];
$to = dirname(__FILE__).'/'.$_FILES['file']['name'];
if( move_uploaded_file($from, $to) ){
echo 'Success';
} else {
echo 'Failure';
}
header('Location: http://www.mywebsite.com/dump/');
exit;
}
?>
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\Temp\phpA30E.tmp' to './people.xml' in E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php on line 3
is the important line it says you can't put the file where you want it and this normally means a permissions problem
check the process running the app (normally the webservers process for php) has the rights to write a file there.
EDIT:
hang on a bit
I jumped the gun a little is the path to the file in the first line correct?
Another think to observe is your directory separator, you are using / in a Windows box..
Add the IIS user in the 'dump' folders security persmissions group, and give it read/write access.
Create a folder named "image" with folder permission 777
<?php
move_uploaded_file($_FILES['file']['tmp_name'],"image/".$_FILES['file']['name']);
?>
We found using below path
{['DOCUMENT_ROOT'] + 'path to folder'
and giving everyone full access to the folder resolved the issue.
Make sure to not reveal the location in the address bar. No sense in giving the location away.

What am I getting a 'Failed to open Stream' error in PHP?

I have been following a set of tutorials in thenewboston [PHP] and I have been using the following code. I am trying to upload a file, more on a 'jpg' file, since it's my first time to upload a file. With this code though, I have been encountering several errors such as the ff:
- move_uploaded_file: failed to open stream: Permission denied
- move_uploaded_file: Unable to move 'tmp_file' to 'file' So, what am I doing wrong?
$name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
if (!empty($name)) {
$folder = 'files/';
if (move_uploaded_file($tmp_name, $folder.'image.jpg')) {
echo 'Uploaded';
}
}
?>
<form method="post" enctype="multipart/form-data" action="untitled%20text.php">
<input type="file" name="file"/> <br/> <br/>
<input type="submit"/>
</form>
--------- Edited (December 23, 2015)
Coming from the answers, I made the conclusion that folders with permissions will produce as an "Failed to open Stream" error because the PHP code (user) has no permission to touch the file.
try like this to change folder permission,
chmod 777 folder_path
refer this link
You can also...
if( !is_dir( $folder) )
{
mkdir( $folder, 0777, TRUE );
}
if you work on localhost try another Drive or Dir or change Dir permission may be it is Write Protected (read only) ... if your program is online change the dir permission from file manager or use .
chmod 777 folder_path

403 error in lighttpd server in my debian linux server while trying to store form data in a text file. How do I solve this

I had created a html form and was trying to store it in a text file using PHP but I kept getting a 403 error. It's most probably because it does not have write permission . How do I give the script the permission to write in the directory? (I'm using the Raspbian Wheezy distro)
<form method="POST" action="store.php">
Enter Your Name: <input type="text" name="fullname" />
</form>
and for the server
<?php
foreach($_POST as $name=>$value)
{
$contents .= "$name = $value" . "\n";
}
// save locally in cache folder
$fd = fopen("cache/filename.txt", "w");
fwrite($fd, $contents);
fclose($fd);
die();
?>
The 403 you're receiving is probably unrelated to the actual PHP code itself. As a general rule, an error in PHP will generate 5xx status codes, not 4xx.
Have you made sure that the webserver account has execute permissions on your PHP file?

Uploading a image to a server + inserting filename to dB

The php:
//This is the directory where images will be saved
$target = "/images/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$strListItemPic=(mysql_real_escape_string($_FILES['photo']['name']));
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). "
has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
The html:
<input type="hidden" name="size" value="350000">
<input type="file" name="photo">
It's breaking out and giving "Sorry, there was a problem uploading your file.";
I'm inserting $strListItemPic later in a MySQL statement, so I can echo the picture in a variable elsewhere.
Any glaring errors in this code? Does my $target have to be absolute paths?
PHP errors:
Warning: move_uploaded_file(/xxxxxx/lists/images/test.gif):
failed to open stream: No such file or directory in
/home/virtual/site48/fst/var/www/html/xxxxxx/lists/itemedit.php on line 22
Warning: move_uploaded_file(): Unable to move '/tmp/phpJ3v7HV' to
'/xxxxxx/lists/images/test.gif'
in /home/virtual/site48/fst/var/www/html/xxxxxx/lists/itemedit.php on line 22
Fixed:
Added enctype="multipart/form-data" to <form>
Removed / before images in $target
Errors don't have to glare in the code.
Errors being raised at the time the code gets executed.
And these errors have to be noted and investigated.
at least add these 2 lines into your code and run it again.
ini_set('display_errors',1);
error_reporting(E_ALL);
or get the error message produced by move_uploaded_file any other way.
Trying to answer your question without an actual error message would be useless waste of time.
Your apache sub directory where you write (move)images must be writable i.e have permissions to write files. since it looks like you are on Linux cd to the directory and change permissions with:
cd /the/parent/directory/to/directory/where/files/are/supposed/to/be
chmod 777 theimagefilesdir/
Be sure to read permissions and choose one that fits you as 777 is very permissive and can be impractical to production but I use it in my local machine :)
Please check that you are using enctype in form tag

PHP move_uploaded_file() issue

I have the PHP script (testupload.php):
<?php
error_reporting(E_ALL);
ini_set('log_errors',1);
ini_set('error_log','/root/work/inputs/log_file');
$target_path = "/work/inputs";
$target_path = $target_path . basename( $_FILES['frag3']['name']);
echo "Received File: " . $_FILES['frag3']['name'] . " and moving it to " . $target_path . "<br>";
if(move_uploaded_file($_FILES['frag3']['name'], $target_path)) {
echo "The file ". basename( $_FILES['frag3']['name']) . " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
And the HTML file to call it:
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form action = "http://localhost:8081/testupload.php" method="post" enctype="multipart/form-data">
<span>Value : </span><input type="text" name="Value" value="Hello world"/><br />
<span>Fragment File : </span><input type="file" name="frag3" /><br />
<input type="submit" value="Send"/>
</form>
</body>
</html>
However, I continually recieve the response:
There was an error uploading the file, please try again!
So it's clear that move_uploaded_file() is not correctly functioning. Plus, it doesn't actually move the file. However, I can't seem to diagnose the issue.
My first thought was that the directory was not writable. But my permissions for the folder are drwxrwxrwx (as determined by ls -l)
Also, the line:
ini_set('error_log','/root/work/inputs/log_file');
doesn't seem to write a log file to that location.
Has anyone had any experience with this? How can I diagnose whats going on here?
I am almost at a loss here, so any help would be greatly appreciated.
I am using OpenSUSE 11.2, Apache 2.2, and PHP 5.3.
Many thanks,
Brett
You should do move_uploaded_file($_FILES['frag3']['tmp_name'], $target_path)
Here are some things to check:
Ensure that your file upload and post limits are not reached by editing upload_max_filesize and post_max_size in your .htaccess or php.ini file. If you have error reporting on, you should see an error when they're reached. See this: http://drupal.org/node/97193
Check for file upload error codes. See the documentation for these: http://www.php.net/manual/en/features.file-upload.errors.php
Ensure that your memory_limit has not been reached. Again, with error logging enabled, you should receive an error message about this. http://drupal.org/node/207036
Check PHP's common pitfalls documentation and make sure there's nothing there that helps: http://www.php.net/manual/en/features.file-upload.common-pitfalls.php
If none of this helps, enable error reporting and post what you receive so we can tailor our answers better to your situation.
the php stores the file in server with temporary name
which you can get by
$_FILES['frag3']['tmp_name'] and not by $_FILES['frag3']['name']
I believe it's move_uploaded_file($_FILES['frag3']['tmp_name'], $target_path), as the files aren't actually stored by the name they're uploaded with (though you can, of course, rename them by setting their target path to such).

Categories