将图像文件上载到同一文件夹

时间:2012-06-05 18:09:39

标签: php

我有一个PHP脚本文件:

<?php
    $error = "";
    $msg = "";
    $fileElementName = 'fileToUpload';
    if(!empty($_FILES[$fileElementName]['error']))
    {
        switch($_FILES[$fileElementName]['error'])
        {

            case '1':
                $error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
                break;
            case '2':
                $error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
                break;
            case '3':
                $error = 'The uploaded file was only partially uploaded';
                break;
            case '4':
                $error = 'No file was uploaded.';
                break;

            case '6':
                $error = 'Missing a temporary folder';
                break;
            case '7':
                $error = 'Failed to write file to disk';
                break;
            case '8':
                $error = 'File upload stopped by extension';
                break;
            case '999':
            default:
                $error = 'No error code avaiable';
        }
    }elseif(empty($_FILES['fileToUpload']['tmp_name']) || $_FILES['fileToUpload']['tmp_name'] == 'none')
    {
        $error = 'No file was uploaded..';
    }else 
    {
            $msg .= " File Name: " . $_FILES['fileToUpload']['name'] . ", ";
            $msg .= " File Size: " . @filesize($_FILES['fileToUpload']['tmp_name']);
            //for security reason, we force to remove all uploaded file
            @unlink($_FILES['fileToUpload']);       
    }       
    echo "{";
    echo                "error: '" . $error . "',\n";
    echo                "msg: '" . $msg . "'\n";
    echo "}";
?>

使用该脚本文件不确定上传图像文件的位置,我想更改此脚本,以便将图像上传到此doajaxfileupload.php文件所在的目录。

2 个答案:

答案 0 :(得分:0)

看这里: http://de2.php.net/manual/de/function.move-uploaded-file.php

<?php
    if (move_uploaded_file[$fileElementName]['tmp_name'], "./" . $fileElementName)) {
       echo "Done!";
    } else {
       echo "Fail";
       //Handle fail
       die(); //Stop
    }
?>

答案 1 :(得分:0)

您没有保存上传的文件,请将$ target_path更改为您想要的文件夹

{
        $target_path = "uploads/";
        $target_path = $target_path . basename( $_FILES['fileToUpload']['name']); 
        move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path

        $msg .= " File Name: " . $_FILES['fileToUpload']['name'] . ", ";
        $msg .= " File Size: " . @filesize($_FILES['fileToUpload']['tmp_name']);
        //for security reason, we force to remove all uploaded file
        @unlink($_FILES['fileToUpload']);       
}