简体   繁体   English

php上传文件到文件夹

[英]php upload file into the folder

I am trying to upload multiple images into the folder using php . 我正在尝试使用php将多个图像上传到文件夹中。 The code can print out the file names which means I get the files but now it does not upload them and I get no error : below is my code 该代码可以打印出文件名,这意味着我可以获取文件,但是现在它没有上传文件,也没有出现错误:下面是我的代码

<?php 
    $target = "image_uploads/";
    if(isset($_FILES['FILE_NAME'])){
        foreach($_FILES['FILE_NAME']['tmp_name']as $key => $error ){
           print_r($key);
           $file_upload = $key.$_FILES['FILE_NAME']['name'][$key];
           #print image names
           echo $file_upload.'</br>';
          move_uploaded_file($file_upload,$target);    
        }
    }
?>

In target you have to give the file name too. 在目标中,您也必须提供文件名。 Please use the code below, 请使用下面的代码,

$target = "image_uploads/";
if(isset($_FILES['FILE_NAME'])){
    foreach($_FILES['FILE_NAME']['tmp_name'] as $key => $error ){
        print_r($key);
        $file_upload = $key.$_FILES['FILE_NAME']['name'][$key];
        print image names
        echo $file_upload.'</br>';
        move_uploaded_file($file_upload,$target.$_FILES['FILE_NAME']['name']);    
    }
}

I think the problem is in the foreach loop. 我认为问题出在foreach循环中。

foreach ($_FILES['FILE_NAME']['tmp_name'] as $key => $val) {
    // this loops through the tmp_name of $_FILES['FILE_NAME']
    // which is a string
}

I think you meant something like: 我认为您的意思是:

foreach ($_FILES as $index => $fileArray) {
    $tmpName = $fileArray['tmp_name'];
    echo "File at key $index is temporarily uploaded at $tmpName";
}

The code above will loop through all uploaded files and print it's current filename. 上面的代码将循环遍历所有上传的文件并打印其当前文件名。

It might happen that your target folder is not writable. 您的目标文件夹可能不可写。

I also think, that the cause of which you're not getting errors is, that you have the following: 我还认为,导致错误的原因是:

print_r($key);

Yous should have: 您应该具有:

print_r($error);

There can be multiple reasons for this : 可能有多种原因:

  1. The target folder must exist before trying to move the file from temp location to the target and must also be writable 尝试将文件从临时位置移动到目标之前,目标文件夹必须存在,并且必须可写

  2. the move_uploaded_file takes the second argument as the file name followed by the directory name, so it can be something like : target folder/user.file.name.ext move_uploaded_file将第二个参数用作文件名,后跟目录名,因此它可能类似于:target folder / user.file.name.ext

  3. If you are uploading multiple files, then the $_FILES must be accessed as shown in the link : http://php.net/manual/en/features.file-upload.multiple.php 如果要上传多个文件,则必须按链接中所示访问$ _FILES: http : //php.net/manual/en/features.file-upload.multiple.php

  4. for the php error messages that you may encounter, here is a list : http://php.net/manual/en/features.file-upload.errors.php 对于您可能遇到的php错误消息,这里是一个列表: http : //php.net/manual/en/features.file-upload.errors.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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