Reputation: 163
Most problems I see are with ONE image input. But what if you have 2 or 3 or more?
My script for one image input worked but I am trying to have one single-image input and one multiple image input in my form. I'm testing out having 2 single-image inputs on my form but not working.
Here is the HTML for one single-image input (assume it is inside a < form > tag):
<input type="hidden" name="size" value="350000"/>
<input type="file" name="schoollogo"/><br/>
The PHP:
$target = "images/logo/";
$target = $target . basename( $_FILES['schoollogo']['name']);
$schoollogo = $_FILES['schoollogo']['name'];
//Writes the information to the database
mysql_query("INSERT INTO Colleges (schoollogo) VALUES ('$schoollogo')") or die(mysql_error()) ;
//Writes the logo to the server
if (move_uploaded_file($_FILES['schoollogo']['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 file.";
}
For the second single-image input, I just added a second input code...
<input type="hidden" name="size" value="350000"/>
<input type="file" name="otherimage"/><br/>
And then added the variable to the PHP:
$targetotherimage = "images/otherimage/";
$targetotherimage = $targetotherimage . basename( $_FILES['otherimage']['name']);
mysql_query("INSERT INTO Colleges (schoollogo,otherimage) VALUES ('$schoollogo','$otherimage')") or die(mysql_error()) ;
//Writes the logo to the server
if (move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target)) && (move_uploaded_file($_FILES['otherimage']['tmp_name'], $targetotherimage)) {
but when adding that code into the PHP, the code does not work. No error messages or anything. The page is blank.
By the way, here is my other question where I am attempting to add a multiple-image upload input into my form with an already-working single-image upload input.
Upvotes: 1
Views: 157
Reputation: 2995
This will work for you, replace your form with this
<form method="post" enctype="multipart/form-data" action="upload.php">
<input type="file" name="file['schoollogo']" >
<input type="file" name="file['otherimage']" >
<input type="submit" value="submit">
</form>
You will get your files in this format:
Array
(
[file] => Array
(
[name] => Array
(
['schoollogo'] => 1465386599_Location_Outline-36.png
['otherimage'] => STONE_Website_v7E.jpg
)
[type] => Array
(
['schoollogo'] => image/png
['otherimage'] => image/jpeg
)
)
)
And in upload.php file
<?php
if (isset($_FILES['file']))
{
$schoollogo=$_FILES['file']['name']['schoollogo'];
$target = "images/logo/".basename($schoollogo);;
$otherimage = $_FILES['file']['name']['otherimage'];
$targetotherimage = "images/otherimage/".basename($otherimage);
$tmp_name_school = $_FILES['file']['tmp_name']['schoollogo'];
$tmp_name_other = $_FILES['file']['tmp_name']['otherimage'];
if ($tmp_name_school != "" && $tmp_name_other !== "")
{
//Upload the file
if(move_uploaded_file($tmp_name_school,$target) && move_uploaded_file($tmp_name_other, $targetotherimage))
{
mysql_query("INSERT INTO Colleges (schoollogo,otherimage) VALUES ('$schoollogo','$otherimage')") or die(mysql_error()) ;
echo'Done!';
}
else
{
echo "Not Moved";
}
}
}
?>
Upvotes: 1
Reputation: 22532
Wrong closing of if condition.
if(move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target)) && (move_uploaded_file($_FILES['otherimage']['tmp_name'], $targetotherimage)) {
^^^^// In your code if condition close here
It is used as
if(move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target)
&& (move_uploaded_file($_FILES['otherimage']['tmp_name'], $targetotherimage)))//if condition close here
{
// rest of code
}
Upvotes: 1