duanne9313 2019-05-07 19:23
浏览 85

发出在不同数组中获取文件数组变量的问题

I am in the process of moving PHP code for a file upload system to its own file, so I can upload the file with AJAX.

I am running into an issue where I can't get this updated code:

$filename       =   $fileArray[$fileNameVar];
$tmp_name       =   $fileArray[$fileTmpNameVar];
$filesize       =   $fileArray[$fileSizeVar];
$file_error     =   $fileArray[$fileErrorVar];
$file           =   $fileArray[$p_img];

To operate how it used to, like this:

$filename       =   $fileArray['file']['name'];
$tmp_name       =   $fileArray['file']['tmp_name'];
$filesize       =   $fileArray['file']['size'];
$file_error     =   $fileArray['file']['error'];
$file           =   $fileArray['file'];

When I try to var_dump anything from the set of new variables, I get nothing showing up in the response.

Does anyone know how I need to setup this array?

Here is the full code:

$p_img = $_FILES['file'];
//var_dump($p_img);

$fileNameVar = $p_img['name'];
$fileTmpNameVar = $p_img['tmp_name'];
$fileSizeVar = $p_img['size'];
$fileErrorVar = $p_img['error'];

//$fileFileVar = $p_img['file'];
try {
// If you make a file function, you can change where things are saved
// You can also change the destination (for portability)
    function UploadFile($fileArray = array(), $destinationFolder = '../project_images/') {
       /* $filename       =   $fileArray['file']['name'];
        $tmp_name       =   $fileArray['file']['tmp_name'];
        $filesize       =   $fileArray['file']['size'];
        $file_error     =   $fileArray['file']['error'];
        $file           =   $fileArray['file'];
       */
        $filename       =   $fileArray[$fileNameVar];
        $tmp_name       =   $fileArray[$fileTmpNameVar];
        $filesize       =   $fileArray[$fileSizeVar];
        $file_error     =   $fileArray[$fileErrorVar];
        $file           =   $fileArray[$p_img];

        var_dump($filename);
        // Save all the default data.
        // Success and error should be set by default to fail
        $return['error']        =   true;
        $return['success']      =   false;
        $return['file']['dest'] =   $destinationFolder.$filename;
        $return['file']['size'] =   $filesize;

        if($file_error == 0)
            $return['error']    =   false;
        // I added a directory creation function so you don't have to 
        // manually make folders. This will do it for you.
        if(!is_dir($destinationFolder))
            mkdir($destinationFolder,0755,true);
        // If your filename is not empty, return success or fail of upload
        if (!empty($filename))
            $return['success']  = (move_uploaded_file($tmp_name, $destinationFolder.$filename));

        return $return;
    }
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
    file_put_contents('error_log_top', "
[{$date->format('Y-m-d H:i:s')}]" . "Error adding attachment: 
" . print_r($e, 1), FILE_APPEND);
}

EDIT: How I call the uplaodFile function:

if(isset($_POST['create'])) {
    // Try uploading
    $upload =   UploadFile($_FILES);
    // If upload fails
    if(!$upload['success']) {
        echo '<h3>Sorry, an error occurred</h3>';
    }
    else {
        // You could add error handling here based on the results of 
        // each function's success or failure below.

        // Try to save it
        $saveToDb   =   SaveToDb($con,$upload['file']['dest']);
        // Get the profile from image name
        $profPic    =   ($saveToDb)? getPhoto($con,$upload['file']['dest']) : false;   
    }
}

Edit - Updated Code:

function UploadFile($fileArray, $destinationFolder = '../project_images/') {
    $fileUploadData = $fileArray['file'];
    $filename       =   $fileUploadData['name'];
    $tmp_name       =   $fileUploadData['tmp_name'];
    $filesize       =   $fileUploadData['size'];
    $file_error     =   $fileUploadData['error'];

    var_dump($filename);
    // Save all the default data.
    // Success and error should be set by default to fail
    $return['error']        =   true;
    $return['success']      =   false;
    $return['file']['dest'] =   $destinationFolder.$filename;
    $return['file']['size'] =   $filesize;

    if($file_error == 0)
        $return['error']    =   false;

    if(!is_dir($destinationFolder))
        mkdir($destinationFolder,0755,true);
    // If your filename is not empty, return success or fail of upload
    if (!empty($filename))
        $return['success']  = (move_uploaded_file($tmp_name, $destinationFolder.$filename));

    return $return;
}
  • 写回答

1条回答 默认 最新

  • dskyx46424 2019-05-07 20:11
    关注

    This: (won't work)

    $filename       =   $fileArray[$fileNameVar];
    $tmp_name       =   $fileArray[$fileTmpNameVar];
    $filesize       =   $fileArray[$fileSizeVar];
    $file_error     =   $fileArray[$fileErrorVar];
    $file           =   $fileArray[$p_img];
    

    Is not the same as this: (will work)

    $filename       =   $fileArray['file']['name'];
    $tmp_name       =   $fileArray['file']['tmp_name'];
    $filesize       =   $fileArray['file']['size'];
    $file_error     =   $fileArray['file']['error'];
    $file           =   $fileArray['file'];
    

    In this code, $fileArray is an array of arrays. Therefore, you can't treat it as a single dimensional array and expect to get actual values from it.

    When you do this:

    $p_img = $_FILES['file'];
    $fileNameVar = $p_img['name'];
    $fileTmpNameVar = $p_img['tmp_name'];
    $fileSizeVar = $p_img['size'];
    $fileErrorVar = $p_img['error'];
    

    Each of these are the actual values. i.e. $fileNameVar contains the actual original name of the file when it was uploaded.

    What you could do is this:

    $fileUploadData = $fileArray['file'];
    $filename       =   $fileUploadData['name'];
    $tmp_name       =   $fileUploadData['tmp_name'];
    $filesize       =   $fileUploadData['size'];
    $file_error     =   $fileUploadData['error'];
    // Other code goes here.
    

    Other thoughts:

    function UploadFile($fileArray = array(), $destinationFolder = '../project_images/')
    

    Should be:

    function UploadFile($fileArray, $destinationFolder = '../project_images/')
    

    It makes no sense to have a default. There is nothing sensible to do in that case if the array is empty.

    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备