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 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题