Unable to do File Upload in PHP

前端 未结 3 1395
离开以前
离开以前 2020-12-22 02:20

I want to upload files in PHP. I wrote

相关标签:
3条回答
  • 2020-12-22 03:06

    Your code isn't even close to working because you're not actually doing any file uploading through PHP. I suggest starting with a PHP file upload tutorial (more) and trying again. If you have any problems with your new code ask for help and show us the code you're having problems with.

    0 讨论(0)
  • 2020-12-22 03:09

    You are a long way from a working script

    You can try

    <?php
    $output = array ();
    $errors = array ();
    $savePath = "uploaded"; // PATH to upload images
    $allowedExt = array (
            "png",
            "jpg" 
    );
    if (isset ( $_FILES ['file'] ) && $_FILES ["file"] ["error"] == UPLOAD_ERR_OK) {
    
        $fileName = $_FILES ['file'] ['name'];
        $fileSize = $_FILES ['file'] ['size'];
        $fileTemp = $_FILES ['file'] ['tmp_name'];
        $fileType = $_FILES["file"]["type"] ;
        $fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
        $fileExt = strtolower ( $fileExt );
    
        //var_dump($fileExt);
    
        if (!in_array ( $fileExt, $allowedExt )) {
            $errors [] = "Invalid File Extention";
        }
    
        if ($fileSize > 800*1024) {
            $errors [] = "File Too large";
        }
    
        if (! is_writable ( $savePath )) {
            $errors [] = "File Destination not writeable";
        }
    
        $fileDst = $savePath . DIRECTORY_SEPARATOR . $fileName;
        $filePrifix = basename ( $fileName, "." . $fileExt );
        $i = 0;
        while ( file_exists ( $fileDst ) ) {
            $i ++;
            $fileDst = $savePath . DIRECTORY_SEPARATOR . $filePrifix . "_" . $i . "." . $fileExt;
    
        }
        if (count ( $errors ) == 0) {
            if (@move_uploaded_file ( $fileTemp, $fileDst )) {
                $output['STATUS'] = "OK";
                $output['TYPE'] = $fileType;
                $output['Size'] = $fileSize;
                $output['Destination'] = $fileDst;
            } else {
                $errors [] = "Error Saving File";
            }
    
        }
    
    
        if(count($errors) > 0)
        {
            echo "<h2>Upload Error</h2>" ;
    
            foreach ($errors as $error)
            {
                echo $error , "<br/>" ;
    
            }
        }
        else
        {
            echo "<h2>File  Uploaded</h2>" ;
            foreach ($output as $key => $value)
            {
                echo $key , ":" , $value , "<br/>" ;
            }
    
        }
    }
    
    ?>
    <br/>
    <br />
    <form method="post" target="_self" enctype="multipart/form-data">
        <input type="file" name="file" id="file" /> <input name="submit"
            type="submit" value="submit" />
    </form>
    
    0 讨论(0)
  • 2020-12-22 03:09

    You need to use POST method in order to get file uploads working.
    So just add method="post" attribute to the form tag and you're done.

    0 讨论(0)
提交回复
热议问题