Jump to content

Multiple file uploads not working properly


edwoli

Recommended Posts

I have a form that uploads new vehicles for sale with the choice of uploading up to 9 images. The images files are moved to a folder and the file name is stored in the database.

 

Currently the file names are being written to the database correctly however the file names are being combined and only one image is uploaded to the specified folder. E.g.;

 

pic1.jpg & pic2.jpg file names are being stored correctly in the database;

photo => pic1.jpg

photo1=> pic2.jpg

 

 

when I look in the image folder only one file is there with the name ‘pic1.jpgpic2.jpg’

 

I am in the first two weeks of learning PHP so not to confuse things but I believe I need to do something with the ‘if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))’ line but all that I have tried has failed.

 

Any help would be much appreciated.

 

Thanks in advance

 

Olly Edwards

 

 

 

 

PHP FILE

 

<?php

//This is the directory where images will be saved

$target = "../images/forsale/";

$target = $target . basename( $_FILES['photo']['name']);

$target = $target . basename( $_FILES['photo1']['name']);

//This gets all the other information from the form

$make=$_POST['make'];

$model=$_POST['model'];

$price=$_POST['price'];

$mileage=$_POST['mileage'];

$year=$_POST['year'];

$fuel=$_POST['fuel'];

$trans=$_POST['trans'];

$reg=$_POST['reg'];

$engine=$_POST['engine'];

$doors=$_POST['doors'];

$seats=$_POST['seats'];

$owners=$_POST['owners'];

$colour=$_POST['colour'];

$interior=$_POST['interior'];

$exterior=$_POST['exterior'];

$mot=$_POST['mot'];

$tax=$_POST['tax'];

$info=$_POST['info'];

$pic1=($_FILES['photo']['name']);

$pic2=($_FILES['photo1']['name']);

 

mysql_connect("localhost", "riemans", "cKaVY5YpJHFHuK7t") or die(mysql_error()) ;

mysql_select_db("riemans") or die(mysql_error()) ;

 

mysql_query("INSERT INTO iyc_forsale (make,model,photo,photo1,price,mileage,year,fuel,trans,reg,engine,doors,seats,owners,colour,interior,exterior,mot,tax,info)

VALUES ('$make', '$model', '$pic1', '$pic2', '$price', '$mileage', '$year', '$fuel', '$trans', '$reg', '$engine', '$doors', '$seats', '$owners', '$colour', '$interior', '$exterior', '$mot', '$tax', '$info')") ;

 

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))

{

 

echo "The file ". basename( $_FILES['uploadedfile']['name']). " This vehicle has been uploaded to the database";

}

else {

 

echo "This vehicle could not be uploaded to the database.";

}

?>

 

FORM FILE

 

<?php

require_once("models/config.php");

securePage($_SERVER['PHP_SELF']);

require_once("models/header.php");

echo "

<body>

<div><img width='500px' src='images/logo_its_your_cloud.png' /></div>

<div id='container'>

<div id='innermenu'>

"; include("nav.php"); echo "

</div>

<div id='innermain'>

<table>

<form method='post' action='addcar.php' enctype='multipart/form-data'>

<tr>

<td align='center'><h2>Car Details</h2></td>

</tr>

<tr>

<td><label>Make</label></td>

<td><input type='text' name='make'/></td>

</tr>

<tr>

<td><label>Model</label></td>

<td><input type='text' name='model'/></td>

</tr>

<tr>

<td><label>Price</label></td>

<td><input type='text' name='price'/></td>

</tr>

<tr>

<td><label>Mileage</label></td>

<td><input type='text' name='mileage' size=30 /></td>

</tr>

<tr>

<td><label>Year</label></td>

<td><input type='text' name='year' size=30 /></td>

</tr>

<tr>

<td><label>Reg Number</label></td>

<td><input type='text' name='reg' size=30 /></td>

</tr>

<tr>

<td><label>Engine</label></td>

<td><input type='text' name='engine' size=30 /></td>

</tr>

<tr>

<td><label>Transmission</label></td>

<td><input type='text' name='trans' size=30 /></td>

</tr>

<tr>

<td><label>Fuel</label></td>

<td><input type='text' name='fuel' size=30 /></td>

</tr>

<tr>

<td><label>Doors</label></td>

<td><input type='text' name='doors' size=30 /></td>

</tr>

<tr>

<td><label>Seats</label></td>

<td><input type='text' name='seats' size=30 /></td>

</tr>

<tr>

<td><label>Owners</label></td>

<td><input type='text' name='owners' size=30 /></td>

</tr>

<tr>

<td><label>Colour</label></td>

<td><input type='text' name='colour' size=30 /></td>

</tr>

<tr>

<td><label>Interior</label></td>

<td><input type='text' name='interior' size=30 /></td>

</tr>

<tr>

<td><label>Exterior</label></td>

<td><input type='text' name='exterior' size=30 /></td>

</tr>

<tr>

<td><label>MOT</label></td>

<td><input type='text' name='mot' size=30 /></td>

</tr>

<tr>

<td><label>Tax</label></td>

<td><input type='text' name='tax' size=30 /></td>

</tr>

<tr>

<td><label>Aditional Information</label></td>

<td><textarea type='text' name='info'></textarea></td>

</tr>

<tr>

<td><label>Photo1</label></td>

<td><input type='hidden' name='size' value='350000'><input type='file' name='photo'></td>

</tr>

<tr>

<td><label>Photo2</label></td>

<td><input type='hidden' name='size' value='350000'><input type='file' name='photo1'></td>

</tr>

<tr>

<td colspan='2' style='padding-top:50px'><input TYPE='submit' name='upload' title='Add data to the Database' value='Add Vechicle To The Database'/></td>

</tr>

</form>

</table>

</div>

<div id='container'>

<div id='mainbottom'>"; include("copy.php"); echo"</div>

</div>

</div>

</body>

</html>";

?>

 

SQL Database

 

PHOTO - VARCHAR (255) - NULL

PHOTO1 - VARCHAR (255) - NULL

Edited by edwoli
Link to comment
Share on other sites

Ah, too late to edit out your password...

 

That code doesn't support uploading multiple images. It just doesn't. And certainly not up to 9. It may look like it's going into the database right but that's the only part that works.

Edited by requinix
Link to comment
Share on other sites

Ah yes, the account details. A very newbie mistake from a newbie, it is not live at the moment so hey ho. Will be more careful in the future.

 

Can you point me in the correct direction to be able to do this? I have attacked learning PHP at the wrong angle and at the moment don’t have the time to go back to basics and learn from the ground up.

 

Thanks again

Link to comment
Share on other sites

I rewrote your scripts a little,

Take a look at that example:

 

form.php

 



<?php
require_once("models/config.php");
securePage($_SERVER['PHP_SELF']);
require_once("models/header.php");
echo "
<body>
<div><img width='500px' src='images/logo_its_your_cloud.png' /></div>
<div id='container'>
<div id='innermenu'>
"; include("nav.php"); echo "
</div>
<div id='innermain'>
<table>
<form method='post' action='addcar.php' enctype='multipart/form-data'>
<tr>
<td align='center'><h2>Car Details</h2></td>
</tr>
<tr>
<td><label>Make</label></td>
<td><input type='text' name='make'/></td>
</tr>
<tr>
<td><label>Model</label></td>
<td><input type='text' name='model'/></td>
</tr>
<tr>
<td><label>Price</label></td>
<td><input type='text' name='price'/></td>
</tr>
<tr>
<td><label>Mileage</label></td>
<td><input type='text' name='mileage' size=30 /></td>
</tr>
<tr>
<td><label>Year</label></td>
<td><input type='text' name='year' size=30 /></td>
</tr>
<tr>
<td><label>Reg Number</label></td>
<td><input type='text' name='reg' size=30 /></td>
</tr>
<tr>
<td><label>Engine</label></td>
<td><input type='text' name='engine' size=30 /></td>
</tr>
<tr>
<td><label>Transmission</label></td>
<td><input type='text' name='trans' size=30 /></td>
</tr>
<tr>
<td><label>Fuel</label></td>
<td><input type='text' name='fuel' size=30 /></td>
</tr>
<tr>
<td><label>Doors</label></td>
<td><input type='text' name='doors' size=30 /></td>
</tr>
<tr>
<td><label>Seats</label></td>
<td><input type='text' name='seats' size=30 /></td>
</tr>
<tr>
<td><label>Owners</label></td>
<td><input type='text' name='owners' size=30 /></td>
</tr>
<tr>
<td><label>Colour</label></td>
<td><input type='text' name='colour' size=30 /></td>
</tr>
<tr>
<td><label>Interior</label></td>
<td><input type='text' name='interior' size=30 /></td>
</tr>
<tr>
<td><label>Exterior</label></td>
<td><input type='text' name='exterior' size=30 /></td>
</tr>
<tr>
<td><label>MOT</label></td>
<td><input type='text' name='mot' size=30 /></td>
</tr>
<tr>
<td><label>Tax</label></td>
<td><input type='text' name='tax' size=30 /></td>
</tr>
<tr>
<td><label>Aditional Information</label></td>
<td><textarea type='text' name='info'></textarea></td>
</tr>
<tr>
<td><label>Photo1</label></td>
<td><input type='hidden' name='size' value='350000'><input type='file' name='photo[]'></td>
</tr>
<tr>
<td><label>Photo2</label></td>
<td><input type='hidden' name='size' value='350000'><input type='file' name='photo[]'></td>
</tr>
<tr>
<td colspan='2' style='padding-top:50px'><input TYPE='submit' name='upload' title='Add data to the Database' value='Add Vechicle To The Database'/></td>
</tr>
</form>
</table>
</div>
<div id='container'>
<div id='mainbottom'>"; include("copy.php"); echo"</div>
</div>
</div>
</body>
</html>";
?>

 

addcar.php

 


<?php

error_reporting(-1);

$conn = mysql_connect('localhost','jazzman','password', 3306) or die(mysql_error());

$db_name = mysql_select_db('test') or die(mysql_error());

//This is the directory where images will be saved
$target = "../images/forsale";

//This gets all the other information from the form
$make=$_POST['make'];
$model=$_POST['model'];
$price=$_POST['price'];
$mileage=$_POST['mileage'];
$year=$_POST['year'];
$fuel=$_POST['fuel'];
$trans=$_POST['trans'];
$reg=$_POST['reg'];
$engine=$_POST['engine'];
$doors=$_POST['doors'];
$seats=$_POST['seats'];
$owners=$_POST['owners'];
$colour=$_POST['colour'];
$interior=$_POST['interior'];
$exterior=$_POST['exterior'];
$mot=$_POST['mot'];
$tax=$_POST['tax'];
$info=$_POST['info'];

// use static values in that case
$pic1= basename($_FILES['photo']['name'][0]);

$pic2= basename($_FILES['photo']['name'][1]);

if(!empty($_FILES['photo']['tmp_name']))
{

 // prepare insert query statement
 $sql = "INSERT INTO iyc_forsale (make,model,photo,photo1,price,mileage,year,fuel,trans,reg,engine,doors,seats,owners,colour,interior,exterior,mot,tax,info)
 VALUES ('$make', '$model', '$pic1', '$pic2', '$price', '$mileage', '$year', '$fuel', '$trans', '$reg', '$engine', '$doors', '$seats', '$owners', '$colour', '$interior', '$exterior', '$mot', '$tax', '$info')";

 // Number of uploaded files
 $num_files = count($_FILES['photo']['tmp_name']);

 /** loop through the array of files ***/
 for($i=0; $i < $num_files;$i++)
 {
	 // check if there is a file in the array
	 if(!is_uploaded_file($_FILES['photo']['tmp_name'][$i]))
	 {
		 $messages[] = 'No file uploaded';
	 }
	 else
	 {
		 // move the file to the specified dir
		 if(move_uploaded_file($_FILES['photo']['tmp_name'][$i],$target.'/'.$_FILES['photo']['name'][$i]))
		 {

			 $messages[] = $_FILES['photo']['name'][$i].' uploaded';

		 }
		 else
		 {
			 // an error message
			 $messages[] = 'Uploading '.$_FILES['photo']['name'][$i].' Failed';
		 }
	 }
 }

 echo '<pre>'.print_r($sql, true).'</pre>';

 echo '<pre>'.print_r($messages, 1).'</pre>';

 // execute query...
 $result = mysql_query($sql) or die(mysql_error());

}
?>

 

Results:

INSERT INTO iyc_forsale (make,model,photo,photo1,price,mileage,year,fuel,trans,reg,engine,doors,seats,owners,colour,interior,exterior,mot,tax,info)

VALUES ('Honda', 'CRV', 'Screenshot.png', '12 Giulio Berruti picture.2.jpg', '11,000', '127 000', '2005', 'GAS', 'Cardan', 'BBK 8761', 'Disel', '4', '5', 'jazzman', 'green', 'interior', 'exterior', 'MOT', '55%', 'Aditional information')

 

Array

(

[0] => Screenshot.png uploaded

[1] => 12 Giulio Berruti picture.2.jpg uploaded

)

 

Edited by jazzman1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.