PHP Form not Uploading all Images to Folder - php

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

Related

PHP trying to move file with move_uploaded_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'];

PHP upload script not functioning and no error on apache logs

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

Rename uploaded file in PHP using move_uploaded_file

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)

Upload multiple files to server with php not uploading to server all files

Hi at all i got a problem i got a form which uploads files and insert into mysql the mysql insert part is working but he only moves one file of the three to the server need please some help here is my code.
The Upload Form:
<form enctype="multipart/form-data" action="add-exposes.php" method="POST"> Expose Name: <input type="text" name="name"><br> Expose Beschreibung: <textarea style="width: 810px; height: 200px" name="expose_desc" class="textarea"></textarea><br> Expose Kategorie:<select name="expose_cat" size="3">
<?php
mysql_connect("xxx.de.mysql", "xxxx", "xxx") or die(mysql_error()) ; mysql_select_db("handwerker_verb") or die(mysql_error()) ;
$data = mysql_query("SELECT * FROM cats") or die(mysql_error());
while($info = mysql_fetch_array( $data )) {
echo '<option value="'.$info['id'].'">'.$info['cat'].'</option>';
} ?>
</select><!--<input type="text" name="expose_cat">--><br>Expose Preis:<input type="text" name="price"> <br> Bild: <input type="file" name="photo"><br> <input type="file" name="photo1"><br> <input type="file" name="photo2"><br> <input type="submit" value="Add"> </form>
Add to server and mysql:
<?php $target = "images/"; $target = $target . basename( $_FILES['photo']['name']);
$target1 = "images/"; $target1 = $target1 . basename( $_FILES['photo1']['name']);
$target2 = "images/"; $target2 = $target2 . basename( $_FILES['photo2']['name']);
$name=$_POST['name']; $expose_desc=$_POST['expose_desc']; $expose_cat=$_POST['expose_cat']; $pic=($_FILES['photo']['name']); $price=$_POST['price']; $pic1=($_FILES['photo1']['name']); $pic2=($_FILES['photo2']['name']);
mysql_connect("xxxx.de.mysql", "xx", "xxx") or die(mysql_error()) ; mysql_select_db("handwerker_verb") or die(mysql_error()) ;
mysql_query("INSERT INTO `exposes` VALUES ('', '$name', '$pic', '$expose_desc', '$expose_cat', '$price', '$pic1', '$pic2' )") ;
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; }
elseif(move_uploaded_file($_FILES['photo1']['tmp_name'], $target1)) { echo "The 2 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; }
elseif(move_uploaded_file($_FILES['photo2']['tmp_name'], $target2)) { echo "The 3 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; }
else { echo "Sorry, there was a problem uploading your file."; } ?>
move_uploaded_file returns true on success, the remaining elseif are never reached.
Try something like this:
$error = false;
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
} else $error = true;
if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target1))
{
echo "The 2 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
} else $error = true;
if(move_uploaded_file($_FILES['photo2']['tmp_name'], $target2))
{
echo "The 3 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
} else $error = true;
if($error)
echo "Sorry, there was a problem uploading your file.";

Failing to upload file

I'm trying to test a simple file upload script but it fails. Here is the html portion.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title="testing"></title>
</head>
<body>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input type="file" name="uploaded"/>
<input type="submit" value="Upload File"/>
</form>
</body>
And below this is the php portion.
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
It fails at this line:
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
And execute the else portion: "Sorry, there was a problem uploading your file." How can I get it to work without failing. Thank you for you help.
The most likely reason is lack of write access to the target directory.
Also, I'd check if $target resolves correctly, as there could be artifacts in the filename that seem to be passed directly to the script and are not filtered.
You could test the permissions with
is_writable( PATH_OF_FOLDER )
I also tend to test the existance of the file with
is_file( PATH_OF_NEW_FILE )
before confirming the upload is successful!

Categories