Trying make file upload with PHP? - php

I'm trying make a file upload using PHP. To do it I'm using $_FILE but I can't understand why does not works. Looking for solutions I'm found some suggestion to use $_FILE, but still can't do this works. To I see if upload works I'm using Postman of Chrome. I'm using Ubuntu with LAMP.
How could I do it ?
<?php
$arquivo = isset($_FILES["file"]) ? $_FILES["file"] : FALSE;
if(!$arquivo) {
echo "You can not access this file directly!";
}else{
$diretorio = "/home/fernando/Imagens/";
if (move_uploaded_file($arquivo["tmp_name"], $diretorio.$arquivo["name"])) {
echo "File upload ok!";
}else{
echo "File not upload!";
}
}
?>
Exception
<br />
<b>Warning</b>: move_uploaded_file(/home/fernando/Imagens/avatar_empresa.jpg): failed to open stream: Permission denied in
<b>/var/www/TelefonesUteis/ws/add_file.php</b> on line
<b>12</b>
<br />
<br />
<b>Warning</b>: move_uploaded_file(): Unable to move '/tmp/phpq9AlVw' to '/home/fernando/Imagens/avatar_empresa.jpg' in
<b>/var/www/TelefonesUteis/ws/add_file.php</b> on line
<b>12</b>
<br />
Postman

Your script doesn't have permissions to add and/or execute files in /home/fernando/Imagens/. Your will have to use chmod:
chmod -R 775 /home/fernando/Imagens

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

upload images and video in server

I want to upload video and images in database on server. I used following code, but when I use this code in my localhost then it works but when I upload through the server, it shows errors.
Here is my code:
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ".basename( $_FILES['uploaded']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
And here are the error messages:
Warning: move_uploaded_file(upload/logo.JPG) [function.move-uploaded-file]: failed
to open stream: Permission denied in C:\Inetpub\vhosts\hiznherzbridalshow.com\httpdocs
\hiznherz_new\test2.php on line 31
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move
'C:\Windows\Temp\php7648.tmp' to 'upload/logo.JPG' in C:\Inetpub\vhosts
\hiznherzbridalshow.com\httpdocs\hiznherz_new\test2.php on line 31
PHP provides a function, chmod() for the task. Change your folder permissions manually or dynamically.
Change your folder permission into 775 or 777
Updated
add this code into your php
chmod("upload", 0777);

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 Error - Uploading a file

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.

Categories