简体   繁体   中英

PHP form are not uploading images

I'm having a problem to upload images using a form. This is my form code :

<?php

    echo '<table width="70%">';
    echo '<tr>';
    echo '<td>Add Promotion</td>';
    echo '</tr>';

echo "<form action='addpromotion_post.php' method='post' enctype='multipart/form-data' name='form1' id='form1'>";


echo '<tr>';
echo "<td>&nbsp;</td>";
echo '</tr>';



echo '<tr>';
echo "<td>Promotion :</td>";
echo "<td><textarea name='promotion1' rows='2'></textarea></td>";
echo '</tr>';

echo '<tr>';
echo "<td>Valid From :</td>";
echo "<td><input type='text' size='40' name='validfrom1' value='promotion' /></td>";
echo '</tr>';



echo '<tr>';
echo "<td>Add Picture :</td>";
echo "<td><input type='file' name='pic1' id='pic1' /></td>";
echo '</tr>';

echo '<tr>';
echo "<td>Add Picture :</td>";
echo "<td><input type='file' name='pic2' id='pic2' /></td>";
echo '</tr>';

echo '<tr>';
echo "<td>Add Picture :</td>";
echo "<td><input type='file' name='pic3' id='pic3' /></td>";
echo '</tr>';


echo '<tr>';
echo "<td>&nbsp;</td>";
echo '</tr>';



echo'<tr>
<td colspan="3" align="center"><input type="submit" name="button" id="button" value="Submit"  /></td>
</tr>
</table>
</form>';

This is my post code :

<?php


    //session_start();
    include_once("connection.php");



    $promotion= $_POST["promotion1"];
    $validfrom= $_POST["validfrom1"];


    $promotion= mysql_real_escape_string($promotion);
    $validfrom= mysql_real_escape_string($validfrom);






    $sql = "insert into promotion(proid, promotion, validfrom) values ('', '$promotion', '$validfrom')";

mysql_query($sql) or die ("Error in query: $sql");

$lastid=mysql_insert_id();






$file = "slider_imagesClient/".$lastid."/";
mkdir($file);



 $pic1 = $_FILES['pic1']['name'];
 $pic2 = $_FILES['pic2']['name'];
 $pic3 = $_FILES['pic3']['name'];

 print_r($pic1);


 $ext1 = pathinfo($pic1,PATHINFO_EXTENSION);
 $ext2 = pathinfo($pic2,PATHINFO_EXTENSION);
 $ext3 = pathinfo($pic3,PATHINFO_EXTENSION);


 //$fileEnote = $_FILES['eNote']['name'];

$path1 = $file.$pic1.'.'.$ext1;
$path2 = $file.$pic2.'.'.$ext2;
$path3 = $file.$pic3.'.'.$ext3;

print_r($path1);


$ory1 = $sfish.$pic1.'.'.$ext1;
$ory2 = $sfish.$pic2.'.'.$ext2;
$ory3 = $sfish.$pic3.'.'.$ext3;


if (!file_exists($path1))
{
 move_uploaded_file ($_FILES['pic1']['tmp_name'], $path1) or die ("Error");
 }

if (!file_exists($path2))
{
 move_uploaded_file ($_FILES['pic2']['tmp_name'], $path2) or die ("Error");
 } 

if (!file_exists($path3))
{
 move_uploaded_file ($_FILES['pic3']['tmp_name'], $path3) or die ("Error");
 }

 if(!empty($pic1)) {
  $sql5= "INSERT INTO pimage (id, proid, image)
            VALUES ('', $lastid, '$ory1')";

  mysql_query($sql5) or die ("Error in query: $sql5");
}
else
{
$not = "not good";
print_r($not);
}

if(!empty($pic2)) {
  $sql5= "INSERT INTO image (resID, image)
            VALUES ($lastid, '$ory2')";

  mysql_query($sql5) or die ("Error in query: $sql5");
}
else
{
$not = "not good";
print_r($not);
}

if(!empty($pic3)) {
  $sql5= "INSERT INTO image (resID, image)
            VALUES ($lastid, '$ory3')";

  mysql_query($sql5) or die ("Error in query: $sql5");
}
else
{
$not = "not good";
print_r($not);
}



?>

print_r($pic1) are not outputting anything. print_r($path1) just outputting slider_imagesClient/50/.

How can I solve this? I'm suspecting error in the form but I couldn't find any. Thank you very much :D. Really appreciate your help.

  echo '<table width="70%">';
  echo '<tr>';
  echo '<td>Add Promotion</td>';
  echo '</tr>';

echo "<form action='addpromotion_post.php' method='post'
 enctype='multipart/form-data' name='form1' id='form1'>";

change like this

echo "<form action='addpromotion_post.php' method='post' enctype='multipart/form-data' name='form1' id='form1'>";
    echo '<table width="70%">';
    echo '<tr>';
    echo '<td>Add Promotion</td>';
    echo '</tr>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM