PHP,有以下两个问题:move\u上传的\u文件

PHP,有以下两个问题:move\u上传的\u文件,php,image,upload,Php,Image,Upload,我最近在尝试用PHP和move_uploaded_file函数上传一些图像时遇到了一些问题 第一个move_上传的_文件工作得很好,但第二个文件不行 我已将其放在页面顶部,但它显示了任何内容: ini_set('display_errors', 1); error_reporting(E_ALL|E_STRICT); 这是我的密码: $folder = 'product/'.$savedProduct -> getId(); //First I create the l

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

第一个move_上传的_文件工作得很好,但第二个文件不行

我已将其放在页面顶部,但它显示了任何内容:

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
        }

    }
稍后我将尝试调整图像大小,谢谢各位。

试试看

 <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
            }
        }
    }
}
?>

用PHP加载多个文件

这将文件从
$tmp\u路径[$cpt]
移动到
$imagePath[$cpt]

move_uploaded_file($tmp_path[$cpt], $imagePath[$cpt]);
move_uploaded_file($tmp_path[$cpt], $thumbnailPath[$cpt]);
这将尝试将文件从
$tmp\u路径[$cpt]
移动到
$thumbnailPath[$cpt]

move_uploaded_file($tmp_path[$cpt], $imagePath[$cpt]);
move_uploaded_file($tmp_path[$cpt], $thumbnailPath[$cpt]);
但是你已经把它移到第一行了,所以没什么可移动的!相反,您必须从其新位置复制它:

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

move_上传的文件()的工作原理类似于“剪切”和“粘贴”。您的第一次呼叫已移动原始文件

它与此组合完美配合:

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