PHP file upload fails when changing HTML input name - php

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)

Related

How to create an upload form with upload and custom filename in php?

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:

File is not uploading i am using apache 2.4.33

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.

warning message shows during the file upload using php

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

File is not uploading giving error "File is not uploaded please try again"?

i'ma beginner and i'm trying to upload a file to uploads/ folder from this tutorial but as i run my program after submit it's showing error There was an error uploading the file, please try again! can you tell me what's wrong here??
form.php:
<html>
<head>
<title>Form image</title>
</head>
<body>
<form enctype="multipart/form-data" action="uploader.php" method="post" name="changer">
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
<input type="file" name="uploadedfile" accept="image/jpeg">
<input type="submit" value="Submit">
</form>
</body>
</html>
uploader.php
<?php
//where the file is going to be placed..
$target_path="uploads/";
$target_path=$target_path.basename($_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['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!";
}
The correct code is
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)
You are using
move_uploaded_file($_FILES['uploadedfile']['name'], $target_path)
This will not work since the file is stored in the temp folder after the upload is initiated and while moving the file it must know the temp name so that the file could be moved from temp folder to the server.

File Upload php

So I am trying to upload files to the database through php here is the code
HTML:
<!doctype html>
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<form action="uploadimage.php" method="POST" enctype="multipart/form-data">
<input type="text" name="tag"/>
<input type="file" name="file"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
php:
<?php
$target = "files/";
$target = $target . basename( $_FILES['file']['name']);
$name=$_POST['tag'];
$file=($_FILES['file']['name']);
mysql_connect("localhost","Bluesir9","Bluesir9","website");
mysql_query("Insert into files values('$name','$file');");
if(move_uploaded_file($_FILES['file']['tmp_name'],$target))
{
echo "The File ".basename($_FILES['file']['name'])."has been uploaded";
}
else
{
echo "Sorry there was an error";
}
?>
when I check the target folder the files are there, but I cant find the files on the database returns an empty result set when I query it through phpMyAdmin.
What am I doing wrong?
Since the target files are there in folder, and if you can't find the files on the database then obvious thing should be to check insert query.
Please check syntax of insert query
mysql_query("Insert into files values('$name','$file')");

Categories