I am using ubuntu os with XAMPP. Here i've created two files uploader.php and phpEx6.php
when i try to upload a file it shows warning message.I am new to php.pls help to solve the problem..
phpEx6.php
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="uploader.php" method="post" enctype="multipart/form- data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
uploader.php
<?php
if( $_FILES['file']['name'] != "" )
{
error_reporting(E_ALL);
move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/opt/lampp/htdocs/xampp/nf/uploads/' $_FILES['file']['name'])
or die("Couldn't copy the file.");
}
else
{
die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>enter code here
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']['type']; ?>
</ul>
</body>
</html>
OUTPUT
Warning: move_uploaded_file(/opt/lampp/htdocs/opt/lampp/htdocs/xampp/nf/uploads/sam.txt): failed to open stream: No such file or directory in /opt/lampp/htdocs/xampp/nf/uploader.php on line 5
Warning:
move_uploaded_file(): Unable to move '/opt/lampp/temp/php1BAGC1' to '/opt/lampp/htdocs/opt/lampp/htdocs/xampp/nf/uploads/sam.txt' in /opt/lampp/htdocs/xampp/nf/uploader.php on line 5
try altering this
move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/opt/lampp/htdocs/xampp/nf/uploads/'. $_FILES['file']['name'])
with this
move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/xampp/nf/uploads/'. $_FILES['file']['name'])
or even this
move_uploaded_file($_FILES['file']['tmp_name'], '/opt/lampp/htdocs/xampp/nf/uploads/'. $_FILES['file']['name'])
The error is about, there's no folder created to which you are trying to move the files.. Create the folder and then try it or check whether the folder has all the permissions needed to upload the file into it..
Here's the sample code for Uploading a file..
if(isset($_POST['Submit']))
{
$tmp_name = $_FILES['Pic']['tmp_name'];
$name = $_FILES['Pic']['name'];
if(is_uploaded_file($tmp_name))
{
$org_name = time()."_".$name;
$dest = "uploads/$org_name";
move_uploaded_file($tmp_name, $dest);
}
}
there can be multiple answer for your questions as followed:
start path with DOT(.) such as './opt/foldername/'
create folder and give write permission to it in which you want to insert your file and data
or you can do before move uploaded in such as way just check first whether directory exist or not if its not than use mkdir function and than chmod to give write permission and than use upload function. its always a better practice to check directory existence and than only move the file there.
move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/xampp/nf/uploads/'. $_FILES['file']['name'])
#Peter Darmis
Related
Hi there I'm trying to create an upload form in php. Containing, upload input, text input (name to be given to the file that was uploaded), and submit button. However I don't know much about php, so I don't know how to actually link that what I had typed on the <input type="text"/> becomes the name of the file when uploaded. If someone can help ? Thanks.
Here is my code:
<!DOCTYPE html>
<html>
<head>
<title>Tu peux uploader ici ta video.</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="text" name="file-name"></input><br /> <!-- [ASK]: How to make this the file name of the uploaded file -->
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?PHP
if(!empty($_FILES['uploaded_file']))
{
$path = "uploads/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
move_uploaded_file ( string $filename , string $destination ) takes filename which is the filename of the uploaded file and destination is the destination of the moved file. Note that destination directory must exist; move_uploaded_file() will not automatically create it. So lets get the name now...
$_FILES['uploaded_file']['tmp_name'] gives you the temporary filename of the file in which the uploaded file was stored on the server eg. C:\Users\USER\AppData\Local\Temp\phpD3C.tmp
while
$_FILES['uploaded_file']['name'] gives you the actual name which you need to extract the extension eg. myfile.jpg
To link that what you had typed on the , first get it via $_POST["file-name"], then concatenate with the extension. Use pathinfo to retrieve the original extension.
Change your code to become...
<!DOCTYPE html>
<html>
<head>
<title>Tu peux uploader ici ta video.</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="text" name="file-name"></input><br /> <!-- [ASK]: How to make this the file name of the uploaded file -->
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(!empty($_FILES['uploaded_file']))
{
$file_parts = pathinfo(basename( $_FILES['uploaded_file']['name'])); //access the actual name instead of tmp_name
//just pick the extension of the file_parts and concatenate it to your path
$path = 'images/';
$path = $path . $_POST["file-name"] . "." . $file_parts['extension'] ;
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename($path) ." has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
}
?>
This should get you the desired name. So if you upload file dog.php and in the text field you have cow, the resulting name should be cow.php.
Result:
I am new to HTML and PHP. I used html/php code from https://gist.github.com/taterbase/2688850 to implement a file upload page on my web server. Created "uploads/" folder in the server and gave chmod 777 permission to it.
It is working when I use name="uploaded_file", the original, I can see the file in the folder. However it is failing when I change the name="xxx" to something else, such as another name or the array form of it, no message on screen, file is not seen in the uploads folder.
Is the "uploaded_file" a fix or hard-coded kind of value? My aim is to use an array instead later, to make it for multiple files upload, however this name change restriction doesn't let me.
Please see below working and not-working samples:
upload.php (name="uploaded_file") Works
<!DOCTYPE html>
<html>
<head>
<title>Upload your files</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?PHP
if(!empty($_FILES['uploaded_file']))
{
$path = "uploads/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
upload.php (name="user_file") Doesn't work
<!DOCTYPE html>
<html>
<head>
<title>Upload your files</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="user_file"></input><br />
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?PHP
if(!empty($_FILES['user_file']))
{
$path = "uploads/";
$path = $path . basename( $_FILES['user_file']['name']);
if(move_user_file($_FILES['user_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['user_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
This line has the problem move_user_file($_FILES['user_file']['tmp_name'], $path).
php's bool move_uploaded_file ( string $filename , string $destination ) moves the uploaded files to a desired location. It is a built in function in php's API.
Your code should be changed to
move_uploaded_file($_FILES['user_file']['tmp_name'], $path)
in order to get it to work.
Your misunderstanding I think has come from your naming. Previously your field name is uploaded_file and the function name is move_uploaded_file(). So you must have thought that, when your field name is user_file, you should call move_user_file() in php. That's kinda creative thinking.. :D
Is the "uploaded_file" a fix or hard-coded kind of value? Well, you need to read the link given.
You are changing the core function call to either undefined function or a user defined function.
If you have defined a function move_user_file(), then its ok.
But, if you are expecting a core PHP inbuilt function, then there is no function move_user_file().
The line seems to have problem:
if(move_user_file($_FILES['user_file']['tmp_name'], $path)) {
Should be:
if(move_uploaded_file($_FILES['user_file']['tmp_name'], $path)) {
You are ending up in calling a undefined function move_user_file().
The required function is: move_uploaded_file()
Its not move_user_file, its move_uploaded_file. Next time, learn how to use your editor's find and replace correctly ;o)
I am trying to use simple php script to upload an image
The code below :
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
<title>Upload your files</title>
</head>
<body>
<form enctype="multipart/form-data" action="uploadfile.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?php
//echo exec('whoami');
//die($_FILES['uploaded_file']);
if(!empty($_FILES['uploaded_file']))
{
$path = "./img/backend/uploads/enquiry/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
I'm using php 7.1 I've already checked the folder for permissions. But when I try to submit it take me to some page with #custom kind of stuff in url. but if I try to post it without image it works fine.
What is the problem ? I think about apache which is not handle that properly. On my other server its working fine.
Just on this server I use auto ssl, is that the problem ?
See the image it take me like this link looks like its not submitting or rejected by server
Please change the path to this
$path = __DIR__."/img/backend/uploads/enquiry/";
You are missing a dot in the file path. Try the following:
$path = "./img/backend/uploads/enquiry/";
Full code:
<!DOCTYPE html>
<html>
<head>
<title>Upload your files</title>
</head>
<body>
<form enctype="multipart/form-data" action="#" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?php
if(!empty($_FILES['uploaded_file'])){
$path = "./img/backend/uploads/enquiry/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
echo $path;
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
Without dot the file path will take script name as well, for example, if your PHP script/file name is "test.php", then without a leading dot in file path will create a path like (www is the folder where your test.php file is saved):
/www/test.php/img/backend/uploads/enquiry/
And hence it will be an invalid file path, with a leading dot it will skip it (layman terms) and your path will be like:
/www/img/backend/uploads/enquiry/
And this path is a valid one, hence it will upload.
I am trying to upload an image though my php script (around 89kb in size). The code for my index.php is:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="file_uploader.php" method="post"
enctype="multipart/form-data">
<input type="file" name="file" size="100" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
This is placed in the same directory as file_uploader.php
and then the code for file_uploader.php is:
<?php
if( $_FILES['file']['name'] != "" )
{
copy( $_FILES['file']['name'], "" ) or
//Exit and show the error message, die does this
die( "Could not copy file!");
}
else
{
die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']['type']; ?>
</ul>
</body>
</html>
The permissions of the directory index and file_uploader are in are : 755.
The script seems to load but I get the error "Could not copy file", not sure what is going on here. Could someone give me some pointers please? Thanks!!
Have a look at the documentation and then please explain what copy( $_FILES['file']['name'], "" ) is supposed to do? If I were you I would fix that statement as well as replace it with a more secure variant that was created specifically for this: move_uploaded_file.
Additionally, read through this section in the manual and you might find another bug in your code; if you'd use $_FILES[0]['tmp_name'] might make it work even better.
Putting all this together; I'd use:
move_uploaded_file($_FILES[0]['tmp_name'], __DIR__ .'/'. $_FILES[0]['name']);
I was trying to upload a csv file in php. I also want it to be able to accept any type of file.
This is the code I'm using, however, it always enters the, "Could not copy file!" condition.
HTML code
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
PHP code
<?php
if( $_FILES['file']['name'] != "" )
{
copy( $_FILES['file']['name'], "/var/www/html/uploads" ) or
die( "Could not copy file!");
}
else
{
die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']['type']; ?>
</ul>
</body>
</html>
I have checked that the uploads folder and the html as well as php files are of permission 777, so they should be able to upload these files
EDIT
I have removed a lot of code from the php file and tried to make it upload(copy) a hardcoded file path from the filesystem.
What I observe is that, if I just run php upload.php from the command line, it works and "uploads" the file from the Downloads onto the Desktop. However,the same upload.php, if run from the browser and via index.html, on one of my servers, it just says, "Could not copy file!". On another server, it downloads the upload.php file. Basically, the same script works from the command line. Doesn't work from the browser.
(I don't see any error message etc on the browser console)
index.html
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
upload.php
<?php
$a = 2;
$b = 1;
if( $a > $b )
{
copy( "/home/PepperBoy/Downloads/myfile.csv", "/home/PepperBoy/Desktop/myfile.csv" ) or die( "Could not copy file!");
}
else
{
die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']['type']; ?>
</ul>
</body>
</html>
Replace this with your same line of code.
copy( $_FILES['file']['tmp_name'], "/var/www/html/uploads" ) or die( "Could not copy file!");
The name value in the $_FILES array doesn't contain the path to the file. Use tmp_name instead. You also need to give the copied file a name not just the destination directory. Also move the file rather than copy it or you will end up cluttering up the tmp directory
move_uploaded_file( $_FILES['file']['tmp_name'], "/var/www/html/uploads/". $_FILES['file']['name'] )
or die( "Could not copy file!");