2016-11-30 48 views
0

我有一個upload.php腳本,我從移動設備訪問,以上傳文檔。PHP的上傳和下載不適用於較大的文件(> 1 MB)

每當我上傳某種圖像,即< 1MB,它可以很好地工作,但是,當上傳一個比這個大的文件時,它會被損壞。

上傳腳本也重命名文件,並從中刪除擴展,如果這可能有什麼關係的錯誤...

這裏的腳本:

<?php header('Access-Control-Allow-Origin: *'); 

    //Read parameters - I use this when I'm adding the file to the database. 
    $documentuniqueid = addslashes($_REQUEST['documentuniqueid']); 
    $type = addslashes($_REQUEST['type']); 
    $notes = addslashes($_REQUEST['notes']); 

    //Get file name 
    $filename = urldecode($_FILES["file"]["name"]); 


    // Check for errors 
    if($_FILES['file']['error'] > 0){ 
     outputJSON('An error ocurred when uploading.'); 
    } 


    // Check filetype 
    //if($_FILES['file']['type'] != 'image/png'){ 
    // outputJSON('Unsupported filetype uploaded.'); 
    //} 

    // Check filesize 
    if($_FILES['file']['size'] > 500000){ 
     outputJSON('File uploaded exceeds maximum upload size.'); 
    } 

    // Check if the file exists 
    if(file_exists('upload/' . $_FILES['file']['name'])){ 
     outputJSON('File with that name already exists.'); 
    } 

    // Upload file 
    if(!move_uploaded_file($_FILES['file']['tmp_name'], 'documents/'.$documentuniqueid)){ 
     outputJSON('Error uploading file - check destination is writeable.'); 
    } 

?> 

下載腳本如下像這樣:

<?php 
    sleep(2); 
    $document_id = addslashes($_REQUEST['document_id']); 

    if($document_id != "") { 
     $real_document_name = GetRealFileName($document_id); 
     if($real_document_name != "ERROR") { 
      $original_filename = "http://www.whatever.com/documents/".$document_id; 
      $new_filename = $real_document_name; 

      //Headers 
      $finfo = finfo_open(FILEINFO_MIME_TYPE); 
      header('Content-Type: '.finfo_file($finfo, $original_filename)); 
      header("Content-Length: " . filesize($original_filename)); 
      header('Content-disposition: attachment; filename="'.$new_filename.'"');  

      //clean up 
      ob_clean(); 
      flush(); 

      readfile($original_filename);   
      exit(); 
     } 
    } 
?> 

有關改進這方面的任何提示? 或者任何見解,爲什麼這是不正確的? 你可以看到我在上傳時將文件重命名爲一個隨機字符串,在下載時將它們重命名,然後查找文件名並將其重命名爲原始文件名。

對小文件大小的預期工作。

我還必須注意,即使我手動進入FTP,下載上傳的文件並自己添加正確的擴展名,我無法打開它們。圖像看起來很糟糕,例如PDF文件已損壞。

PHP詳情:

兩個upload_tmp_dirpost_max_size以及upload_max_filesize的設置爲100M。 max_file_uploads設置爲20和file_uploads爲ON max_input_time設置:60 的max_execution_time:3000

+1

運行的phpinfo(),並檢查您的設置...如的post_max_size ...閱讀更多http://stackoverflow.com/questions/7754133/php-post-max-size- overrides-upload-max-filesize – donald123

+0

嗨唐納德! post_max_size是100M。我認爲這不是問題,應該是我在代碼中忽略的東西。 – Laureant

+0

嘗試通過FTP上傳,然後檢查您是否看到任何錯誤消息 –

回答

0

我覺得你沒有足夠的空間,在你的主機,嘗試通過FTP "FileZilla for example"上傳和查看日誌信息

也做不要忘記檢查這些值

  • 的upload_max_filesize
  • 的post_max_size
  • max_input_time設置
  • 的max_execution_time
0

爲了保持原始文件的名稱,你需要改變

move_uploaded_file($ _ FILES [ '文件'] [ 'tmp_name的值'], '文件/'。$ documentuniqueid)

move_uploaded_file - move_uploaded_file($ _ FILES [ '文件'] [ 'tmp_name的值'],$ _FILES [ '文件'] [ '名'])

或寫line in upload script to see the information about the file

$_FILES - print_r($ _ FILES);

對不起,我的英語,我希望我幫助。

2

/etc/php5/apache2/以內增加php.ini文件的上傳文件屬性的大小。

集:

upload_max_filesize = 50M 
+0

upload_max_filesize已設置爲100M – Laureant

相關問題