2012-04-24 57 views
1

所以我測試了w3schools網站http://www.w3schools.com/php/php_file_upload.asp的move_uploaded_file()php腳本。 這是我的代碼。php move_uploaded_file()

if ($_FILES["file"]["size"] < 2000000) 
{ 
    if ($_FILES["file"]["error"] > 0) 
     echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; 
    else 
    { 
     echo "Upload: " . $_FILES["file"]["name"] . "<br />"; 
     echo "Type: " . $_FILES["file"]["type"] . "<br />"; 
     echo "Size: " . ($_FILES["file"]["size"]/1024) . " Kb<br />"; 
     echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; 

     if (file_exists("/var/www/upload/" . $_FILES["file"]["name"])) 
     { 
      echo $_FILES["file"]["name"] . " already exists. "; 
     } 
     elseif(move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/upload/".$fileName)) 
      echo "Stored in: " . "/var/www/upload/".$fileName; 
    } 
} 
else 
    echo "Invalid file"; 

問題是if(move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/upload/".$fileName))返回false所有的時間,但似乎該文件存儲在服務器(例如:/tmp/php8rrKoW)上tmp文件夾。當我檢查tmp文件夾時,該文件不存在。 (它應該在腳本執行完成後被刪除。)我也沒有看到/php8rrkoW文件夾。我不確定它是否應該在那裏。我使用chmodtmp文件夾和/var/www/upload/的許可設置爲777,但我不確定是否應該將所有者設置爲apache。所以我想知道爲什麼該文件不會被複制到/var/www/upload以及是否有方法來測試。

+4

http://w3fools.com/ – Marty 2012-04-24 04:20:00

+0

你是否啓用了error_reporting(E_ALL),你得到了什麼錯誤? – 2012-04-24 04:37:14

+0

嘗試使用'<?php file_put_contents('/ var/www/upload/test.txt','test');''請求'index.php'。您是否看到任何錯誤,並且文件是否成功創建? – Casey 2015-01-02 23:48:25

回答

0

看起來您應該使用./upload/而不是/ var/www/upload /,因爲您已經在可訪問的網站的主目錄中。

您也可以參考http://www.tizag.com/phpT/fileupload.php 或API:http://php.net/manual/en/function.move-uploaded-file.php

讓我知道是否可行

+0

我嘗試了tizag的代碼,並將其改爲./upload,但我得到了「上傳文件時出現錯誤,請重試!」。我不確定文件是否被轉移到臨時文件夾。 – user1352777 2012-04-24 05:12:05

1

這是一個基本的圖像上傳類我的另一個問題前些天,使用簡單做,也許你覺得它很有趣。

<?php 
error_reporting(E_ALL); //Will help you debug a [server/path/permission] issue 
Class uploadHandler{ 
    public $upload_path; 
    public $full_path; 
    public $name; 
    public $size; 
    public $ext; 
    public $output; 
    public $input; 
    public $prefix; 
    private $allowed; 

    function upload(){ 
     if($_SERVER['REQUEST_METHOD'] == 'POST'){ 
      if(isset($_FILES[$this->input]['error'])){ 
       if($_FILES[$this->input]['error'] == 0){ 
        $this->name  = basename($_FILES[$this->input]['name']); 
        $file_p   = explode('.', $this->name); 
        $this->ext  = end($file_p); 
        $this->full_path = rtrim($this->upload_path,'/').'/'.preg_replace('/[^a-zA-Z0-9.-]/s', '_', $this->prefix.'_'.$file_p[0]).'.'.$this->ext; 
        $info   = getimagesize($_FILES[$this->input]['tmp_name']); 
        $this->size  = filesize($_FILES[$this->input]['tmp_name']); 

        if($info[0]>$this->allowed['dimensions']['width'] || $info[1] > $this->allowed['dimensions']['height']){ 
         $this->output = 'File dimensions too large!'; 
        }else{ 
         if($info[0] > 0 && $info[1] > 0 && in_array($info['mime'],$this->allowed['types'])){ 
          move_uploaded_file($_FILES[$this->input]['tmp_name'],$this->full_path); 
          $this->output = 'Upload success!'; 
         }else{ 
          $this->output = 'File not supported!'; 
         } 
        } 
       }else{ 
        if($_FILES[$this->input]['error']==1){$this->output = 'The uploaded file exceeds the upload_max_filesize directive!';} 
        if($_FILES[$this->input]['error']==2){$this->output = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in our HTML form!';} 
        if($_FILES[$this->input]['error']==3){$this->output = 'The uploaded file was only partially uploaded!';} 
        if($_FILES[$this->input]['error']==4){$this->output = 'No file was uploaded!';} 
        if($_FILES[$this->input]['error']==6){$this->output = 'Missing a temporary folder!';} 
        if($_FILES[$this->input]['error']==7){$this->output = 'Failed to write uploaded file to disk!';} 
        if($_FILES[$this->input]['error']==8){$this->output = 'A PHP extension stopped the file upload!';} 
       } 
      } 
     } 
    } 

    function setPath($var){ 
     $this->upload_path = $var; 
    } 
    function setAllowed($var=array()){ 
     $this->allowed = $var; 
    } 
    function setFilePrefix($var){ 
     $this->prefix = preg_replace('/[^a-zA-Z0-9.-]/s', '_', $var); 
    } 
    function setInput($var){ 
     $this->input = $var; 
    } 

} 



//Start class 
$upload = new uploadHandler(); 
//Set path 
$upload->setPath('./'); 
//Prefix the file name 
$upload->setFilePrefix('user_uploads'); 
//Allowed types 
$upload->setAllowed(array('dimensions'=>array('width'=>200,'height'=>200), 
          'types'=>array('image/png','image/jpg','image/gif'))); 
//form property name     
$upload->setInput('myfile'); 
//Do upload 
$upload->upload(); 


//notice 
if(isset($upload->output)){ 
    echo $upload->output; 
} 
?> 

<form action="" method="POST" enctype="multipart/form-data"> 
    <!--1 MB = 1048576 bytes--> 
    <input type="hidden" name="MAX_FILE_SIZE" value="1048000" /> 

    <p>Upload your image:<input type="file" name="myfile"><input type="submit" value="Upload"></p> 

</form> 
+0

我得到'上傳成功'的消息,但我不能找到上傳的文件。該文件將存儲在哪個位置?我在/var/www/html/test/index.php中部署了代碼。 – 2016-02-24 05:41:43

0

要將文件移動到的目標目錄應該可由Web服務器用戶寫入。另外不要忘記,一些web服務器在changeroot內運行,因此可能不需要目標路徑的一部分。 PHP幫助文檔還說你應該檢查HTLM表單以確保它的enctype ='multipart/form-data'。

最後:$ filename的定義在哪裏?

0

你的路必須由一個「/ home/USR名」

嘗試增加一個「/ home /你的戶名」到「/無功/網絡/上傳」的開始。

爲了得到一個想法,在你的根目錄下添加一個info.php,在瀏覽器中打開它並查看'Loaded Configuration File'。