PHP,以下两个move_uploaded_file的问题

时间:2014-10-21 09:37:21

标签: php image upload

我最近在尝试使用PHP和move_uploaded_file函数上传某些图片时遇到了一些问题。

第一个move_uploaded_file工作正常但不是第二个。

我把它放在我的页面顶部,但它显示了任何内容:

ini_set('display_errors', 1);
error_reporting(E_ALL|E_STRICT);

这是我的代码:

    $folder = 'product/'.$savedProduct -> getId();

    //First I create the list of my path, starting with my main picture

    $imagePath[0] = '../uploaded_pics/'.$folder.'/'.$_FILES['main_image']['name'];
    $thumbnailPath[0] = '../uploaded_pics/'.$folder.'/thumbnail/'.$_FILES['main_image']['name'];
    $tmp_path[0] = $_FILES['main_image']['tmp_name'];

    for ($cpt = 1; $cpt < 6; $cpt ++) { //Because I only want 5 pics and start at 1 so I won't erase the Main Image datas.

        if (isset($_FILES['thumbnail_image_'.$cpt])) { //It refer to the HTML file input name thumbnail_image_someNumber

            $imagePath[$cpt] = '../uploaded_pics/'.$folder.'/'.$_FILES['thumbnail_image_'.$cpt]['name'];
            $thumbnailPath[$cpt] = '../uploaded_pics/'.$folder.'/thumbnail/'.$_FILES['thumbnail_image_'.$cpt]['name'];
            $tmp_path[$cpt] = $_FILES['thumbnail_image_'.$cpt]['tmp_name'];

        }

    }

    //Then I check-create my folders

    if (!is_dir('../uploaded_pics/'.$folder)) {

        mkdir('../uploaded_pics/'.$folder, 0777, true);

    }

    if (!is_dir('../uploaded_pics/'.$folder.'/thumbnail')) {

        mkdir('../uploaded_pics/'.$folder.'/thumbnail', 0777, true);

    }

    //And Then I save my pictures

    if (count($imagePath) > 1) { //If I have more than just the Main Image

        for ($cpt = 0; $cpt < count($imagePath); $cpt ++) {

            $image = new Image('', $imagePath[$cpt], $thumbnailPath[$cpt]);

            saveImage($image, $bdd); //Save the path in MySQL database, this works fine

            move_uploaded_file($tmp_path[$cpt], $imagePath[$cpt]);
            move_uploaded_file($tmp_path[$cpt], $thumbnailPath[$cpt]);
            //And Here happened the problem
        }

    }

只有第一个move_uploaded_file正在运行,下一个没有做任何事情。我尝试var_dumped数组,它们都包含正确的数据。我也尝试将两个move_uploaded_file反转,因此它们分离得很好,只是当它们都被“激活”时才这样。

EDIT /溶液

只需要将第二个move_uploaded_file替换为:

copy($imagePath[$cpt], $thumbnailPath[$cpt]);

我会稍后尝试调整图片大小,谢谢你们。

4 个答案:

答案 0 :(得分:0)

尝试

 <html lang="en">
    <head>
      <meta charset="UTF-8" />
      <title>Multiple File Ppload with PHP</title>
    </head>
    <body>
      <form action="" method="post" enctype="multipart/form-data">
        <input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
      <input type="submit" value="Upload!" />
    </form>
    </body>
    </html>
<?php
$valid_formats = array("jpg", "png");
$max_file_size = 1280*1024; 
$path = "uploads/"; // Upload directory
$count = 0;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    // Loop $_FILES to exeicute all files
    foreach ($_FILES['files']['name'] as $f => $name) {     
        if ($_FILES['files']['error'][$f] == 4) {
            continue; // Skip file if any error found
        }          
        if ($_FILES['files']['error'][$f] == 0) {              
            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$name is too large!.";
                continue; // Skip large files
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                $message[] = "$name is not a valid format";
                continue; // Skip invalid file formats
            }
            else{ // No error found! Move uploaded files 
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                $count++; // Number of successfully uploaded file
            }
        }
    }
}
?>

答案 1 :(得分:0)

这会将文件从$tmp_path[$cpt]移至$imagePath[$cpt]

move_uploaded_file($tmp_path[$cpt], $imagePath[$cpt]);

这会尝试将文件从$tmp_path[$cpt]移至$thumbnailPath[$cpt]

move_uploaded_file($tmp_path[$cpt], $thumbnailPath[$cpt]);

但是你已经把它移到了第一行,所以没有什么可动的!相反,您必须从新位置复制它:

copy($imagePath[$cpt], $thumbnailPath[$cpt]);

答案 2 :(得分:0)

move_uploaded_file()就像&#34; cut&#34;并且&#34;粘贴&#34;。您的第一个电话已经移动了原始文件。

答案 3 :(得分:0)

它与这个组合完美配合:

move_uploaded_file($tmp_path[$cpt], $imagePath[$cpt]);
copy($imagePath[$cpt], $thumbnailPath[$cpt]);