I have this code and would like to rename the uploaded image's name!
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploadedfile" accept="image/*" capture>
<input type="submit" value="Upload">
</form>
<?php
$target_path = "upload/";
$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!";
}
?>
What can i do?
When using move_uploaded_file($filename, $destination) you can specify the name of the file at the destination.
$target_path = "upload/";
$target_filename = 'filename.xyz'
$target_path = $target_path . $target_filename);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". $target_filename).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
cf. move_uploaded_file($filename, $destination)
Related
I have an error. I made an file upload in HTML 5 and PHP. When I try to upload anything there always appears an error. Here's the code:
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload your file" name="submit">
</form>
upload.php:
$target_path = "upload/";
$target_path = $target_path . basename($_FILES["fileToUpload"]["name"]);
if(isset($_POST["submit"])) {
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_path)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
As mentioned in the comment, the variable used in the logic test was not defined anywhere. I believe you were intending something like this.
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload your file" name="submit">
</form>
<?php
$target_path = "upload/";
$target_file = $target_path . basename( $_FILES["fileToUpload"]["name"] );
if( isset($_POST["submit"]) ) {
if ( file_exists( $target_file ) ) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ( move_uploaded_file( $_FILES["fileToUpload"]["tmp_name"], $target_file ) ) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"] ). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
Just change the filename in the target portion of the move_uploaded_file call
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
I have been back forward about this, can't find a solution. My file that I am trying to upload is not moving. I'm using WAMP and my root folder is C:\wamp\www
I've checked that the directory exists in php and put in a script that if it does not exist it should be created, which works, but still it is not moving the file. What am I doing wrong?
The Form:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
Upload.php
$target_path = "uploads/" ;
if(!file_exists($target_path)) {
mkdir($target_path, 0755, true);
echo "The directory was created";
}
$tmp_name = $_FILES['file']['tmp_name'];
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
echo $target_path;
if(move_uploaded_file($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!";
}
try to change,
$tmp_name = $_FILES['file']['tmp_name'];
to
$tmp_name = $_FILES['uploadedfile']['tmp_name'];
I created an upload script using the resources from tizag, the script is returning an onscreen error however no error in the apache logs.
HTML Form
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="90000000" />
Select video to upload:
Please choose a file: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
PHP Code
<?php
$target_path = "/var/www/html/upload/";
$target = $target_path . basename($_FILES['uploadedfile']['name'][0] );
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'] [0], $target_path))
{
echo "The file ". basename( $_FILES['uploadefile']['name'] [0]). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
This shouldn't be very hard to impliment, but with no errors on apache it's very hard for me to troubleshoot. My php knowlegdge is limited so please bare that in mind.
Kind Regards,
Mark Couto
This line:
$target = $target_path . basename($_FILES['uploadedfile']['name'][0] );
As it stands, $target is just a stray variable and not being used anywhere else.
It should read as:
$target_path = $target_path . basename($_FILES['uploadedfile']['name'][0] );
"I created an upload script using the resources from tizag"
The Tizag tutorial you followed doesn't change their variables.
Their example:
$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!";
}
Source: http://www.tizag.com/phpT/fileupload.php
Plus, you have a typo in ['uploadefile'] which should read as ['uploadedfile']
First ensure that php is configured to allow file upload.
in your "php.ini" file search for the directive, and set it ON,
file_uploads= ON
Source : http://www.w3schools.com/php/php_file_upload.asp
I have a PHP form that is supposed to allow the user to upload 5 images at once. All images should save into my website's folder images/ when the user hits submit. Currently, only the image in the first image input gets saved into the images/ folder. The image names are all saved into my MySQL table correctly, though.
Here is the code for my HTML form page:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<form enctype="multipart/form-data" action="add.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br>
Phone: <input type="text" name = "phone"><br>
Photo: <input type="file" name="photo"><br>
Photo: <input type="file" name="photo1"><br>
Photo: <input type="file" name="photo2"><br>
Photo: <input type="file" name="photo3"><br>
Photo: <input type="file" name="photo4"><br>
<input type="submit" value="Add">
</form>
</body>
</html>
And here is the add.php page code:
<?php
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
$target1 = "images/";
$target1 = $target1 . basename( $_FILES['photo1']['name']);
$target2 = "images/";
$target2 = $target2 . basename( $_FILES['photo2']['name']);
$target3 = "images/";
$target3 = $target3 . basename( $_FILES['photo3']['name']);
$target4 = "images/";
$target4 = $target4 . basename( $_FILES['photo4']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);
$pic1=($_FILES['photo1']['name']);
$pic2=($_FILES['photo2']['name']);
$pic3=($_FILES['photo3']['name']);
$pic4=($_FILES['photo4']['name']);
// Connects to your Database
mysql_connect("dnsacom", "ksbm", "Kszer") or die(mysql_error()) ;
mysql_select_db("keabm") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic', '$pic1', '$pic2', '$pic3', '$pic4')") ;
//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 image.";
}
if(move_uploaded_file($_FILES['photo1']['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 image.";
}
if(move_uploaded_file($_FILES['photo2']['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 image.";
}
if(move_uploaded_file($_FILES['photo3']['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 image.";
}
if(move_uploaded_file($_FILES['photo4']['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 image.";
}
?>
Thank you for any help. All help is appreciated.
They all have the same $target variable. Change your $target variables to $target1, $target2, $target3 and $target4 and they'll upload.
if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target1)) <--- CHANGE THESE
Edit: I tested your code and changed these variables and was able to upload all images.
maybe check map permissions, i recommend checking out chmod, i can't find a problem in your code.
http://nl1.php.net/chmod
or maybe use brackets since you upload multiple files? like type="file[]"
OW
if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target)) problem solved you use $target everywhere which is why only 1 picture gets uploaded
I have two files (html and php). The user uploads a file in the html form and the php file contains a script to upload the file onto a server.
It works perfectly, my problem is when User 1 uploads 'wordThing.docx' and User 2 comes along and uploads 'wordThing.docx', User 1's file will be overridden.
Here's my HTML code:
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br />
<label for="yourName">Your name:</label>
<input type="textbox" name="yourName" id="yourName" /><br />
<input type="submit" name="submit" value="Submit"><br />
</form>
</body>
</html>
And this is my PHP script:
<?php
$userName=$_POST['yourName'];
$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!";
}
?>
I would like to attach the text the user would enter into 'yourName'. Thus the server has contains different file names and none has to be overridden.
So assuming that the users have different names, I would like to know how do I save the file on the server with a name attached to it. For example: User 1's file upload would be 'wordThingSusan.docx'
I hope this isn't too much info. I just wanted to be clear and precise.
Just in case someone is trying to use this code, you need to have a folder named 'uploads' under your directory for it to work.
change
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
to
$target_path = $target_path . time() . rand(11, 99) . basename( $_FILES['uploadedfile']['name']);
The generated name will be something like uploads/123456789052wordThing.docx
time() will give you the time in format 1234567890, rand(11,99) will generate random number between 11 and 99, so even if 2 people upload same file at same time, the file won't be overwritted.
Just add a unix timestamp to the beginning of the filename:
<?php
$userName=$_POST['yourName'];
$target_path = "uploads/";
$target_path = $target_path . date('U') . '_' . 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!";
}
?>
I would look into the file_exists method:
http://php.net/manual/en/function.file-exists.php
If the check came back that the file already exists I would rename the file and then save it with the new name.
That is how I would attempt it.