douchuntang2827 2015-02-19 12:55
浏览 36
已采纳

上传多个图片

I have a simple code to upload multiple images that uploads the image to a folder and saves the path to the database. The problem I have is that the image names are will saved to the database but the images are not uploaded to the folder. Here is the code I am using. Its a free hand code( with not too much of html) so you can practically try it. My Database consists of id, image1, image2, image3.

Here is the code for upload

<?php
include'includes/db.php';
  if(isset($_POST['submit'])){

    $extension = substr($_FILES['photo1']['name'],
    strrpos($_FILES['photo1']['name'], '.'));

    $extension = substr($_FILES['photo2']['name'],
    strrpos($_FILES['photo2']['name'], '.'));

    $extension = substr($_FILES['photo3']['name'],
    strrpos($_FILES['photo3']['name'], '.'));


     $extension = strtolower($extension);
     echo $extension;

    if( $extension == ".jpg" || $extension == ".jpeg" || $extension ==  ".gif" || $extension == ".png" )
    {
        $img1=$_FILES['photo1']['name'];
        $img2=$_FILES['photo2']['name'];
        $img3=$_FILES['photo3']['name'];

        $size=$_FILES['photo']['size'];
        $type=$_FILES['photo']['type'];
        $temp=$_FILES['photo']['tmp_name'];

        $limit_size = 1024000; 
        $size_in_kb = 1024; 
        $max_size = $limit_size/$size_in_kb; 


        if($size > $limit_size)
        {
            echo "<script>location.replace('test.php?err=File size exceeds $max_size KB')</script>";    

        }
        else 
        {
            move_uploaded_file($temp,"images/".$img1);
            move_uploaded_file($temp,"images/".$img2);
            move_uploaded_file($temp,"images/".$img3);

            $sql2="INSERT INTO ad_images(image1, image2, image3)VALUES('$img1', '$img2', '$img3')";
            $res2=mysql_query($sql2);

            if($res2){
            echo "<script>location.replace('test.php?success=Product added successfuly')</script>";
            }else{
            echo "<script>location.replace('test.php?vlx=Error. Try Again...')</script>";
        }
     }
   }
 }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Script Testing</title>
</head>

<body>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
  <p> Upload Image<br />
        <input type="file" name="photo1" id="photo"><br />
        <input type="file" name="photo2" id="photo"><br />
        <input type="file" name="photo3" id="photo"><br />

    <input type="submit" name="submit" id="submit" value="Add Product" style="margin-top: 25px; margin-left: 335px;"/>
  </p>

</body>
</html>

Everything seems fine but still the images are not uploaded to the specified folder. Please help me out guys.

*****SOLVED****** SOLUTION: I just had to place this in my code.

$temp1=$_FILES['photo1']['tmp_name'];
$temp2=$_FILES['photo2']['tmp_name'];
$temp3=$_FILES['photo3']['tmp_name'];

Here is what I did...

<?php
include'includes/db.php';
  if(isset($_POST['submit'])){

    $extension = substr($_FILES['photo1']['name'],
    strrpos($_FILES['photo1']['name'], '.'));

    $extension = substr($_FILES['photo2']['name'],
    strrpos($_FILES['photo2']['name'], '.'));

    $extension = substr($_FILES['photo3']['name'],
    strrpos($_FILES['photo3']['name'], '.'));


     $extension = strtolower($extension);
     echo $extension;

    if( $extension == ".jpg" || $extension == ".jpeg" || $extension ==  ".gif" || $extension == ".png" )
    {
        $img1=$_FILES['photo1']['name'];
        $img2=$_FILES['photo2']['name'];
        $img3=$_FILES['photo3']['name'];

        $size=$_FILES['photo']['size'];
        $type=$_FILES['photo']['type'];

        $temp1=$_FILES['photo1']['tmp_name'];
        $temp2=$_FILES['photo2']['tmp_name'];
        $temp3=$_FILES['photo3']['tmp_name'];

        $limit_size = 1024000; 
        $size_in_kb = 1024; 
        $max_size = $limit_size/$size_in_kb; 


        if($size > $limit_size)
        {
            echo "<script>location.replace('test.php?err=File size exceeds $max_size KB')</script>";    

        }
        else 
        {
            move_uploaded_file($temp1,"images/".$img1);
            move_uploaded_file($temp2,"images/".$img2);
            move_uploaded_file($temp3,"images/".$img3);

            $sql2="INSERT INTO ad_images(image1, image2, image3)VALUES('$img1', '$img2', '$img3')";
            $res2=mysql_query($sql2);

            if($res2){
            echo "<script>location.replace('test.php?success=Product added successfuly')</script>";
            }else{
            echo "<script>location.replace('test.php?vlx=Error. Try Again...')</script>";
        }
     }
   }
 }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Script Testing</title>
</head>

<body>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
  <p> Upload Image<br />
        <input type="file" name="photo1" id="photo"><br />
        <input type="file" name="photo2" id="photo"><br />
        <input type="file" name="photo3" id="photo"><br />

    <input type="submit" name="submit" id="submit" value="Add Product" style="margin-top: 25px; margin-left: 335px;"/>
  </p>

</body>
</html>

now the images are uploaded well to the folder and also saved the name to the database. Thanks to Slavic for pointing it out.

  • 写回答

3条回答 默认 最新

  • douyouchou1085 2015-02-19 13:02
    关注

    It seems you are attempting to move a file that was not uploaded.

    <input type="file" name="photo1" id="photo"><br />
    <input type="file" name="photo2" id="photo"><br />
    <input type="file" name="photo3" id="photo"><br />
    

    Then in your PHP you have:

    $temp=$_FILES['photo']['tmp_name'];
    // this would have to be something you have uploaded in your form:
    // $_FILES['photo1']['tmp_name']; 
    

    Usually Power Programming helps in these cases. Use a buddy (he doesn't have to be better than you at programming, but it does help if he is) and explain to him the problem as well as you can. Next thing you know - you've solved your issue.

    P.S. Unrelated to your explicit problem, one should not have duplicates of id values in html. You have three ids that are duplicates.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。