2014-05-23 114 views
6

我在嘗試將文件上傳到我的網絡服務器時出現錯誤。他們正在上傳正常,這是當我從文件中提取細節時,它會引發錯誤。也可以點擊上傳文件時,它把它帶來了'./filename.jpg''uploads/filename.jpg'上傳文件夾是/uploads/警告:filemtime():stat失敗

我相信這是處理文件的真實路徑,但我不確定如何解決它在錯誤的道路。

這是我使用的代碼示例。

<form enctype="multipart/form-data" action="./.upload_file.php" method="post"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> 
    Choose a file to upload: <input name="uploaded_file" type="file" /> 
    <input type="submit" value="Upload" /> 
    </form> 

    <table class="sortable"> 
     <thead> 
     <tr> 
      <th>Filename</th> 
      <th>Type</th> 
      <th>Size</th> 
      <th>Date Modified</th> 
      <th>Delete File?</th> 
     </tr> 
     </thead> 
     <tbody> 
     <?php 


    // Adds pretty filesizes 
    function pretty_filesize($file) { 
     $size=filesize($file); 
     if($size<1024){$size=$size." Bytes";} 
     elseif(($size<1048576)&&($size>1023)){$size=round($size/1024, 1)." KB";} 
     elseif(($size<1073741824)&&($size>1048575)){$size=round($size/1048576, 1)." MB";} 
     else{$size=round($size/1073741824, 1)." GB";} 
     return $size; 
    } 



    // Checks to see if veiwing hidden files is enabled 
    if($_SERVER['QUERY_STRING']=="hidden") 
    {$hide=""; 
    $ahref="./"; 
    $atext="Hide";} 
    else 
    {$hide="."; 
    $ahref="./?hidden"; 
    $atext="Show";} 



    // Opens directory 
    $path = "./uploads/"; 
    $myDirectory=opendir("$path"); 
    echo $myDirectory .'My directory'; 
    echo '<br/>'.$path; 

    // Gets each entry 
    while($entryName=readdir($myDirectory)) { 
     $dirArray[]=$entryName; 
    } 

    // Closes directory 
    closedir($myDirectory); 

    // Counts elements in array 
    $indexCount=count($dirArray); 

    // Sorts files 
    sort($dirArray); 

    // Loops through the array of files 
    for($index=0; $index < $indexCount; $index++) { 

    // Decides if hidden files should be displayed, based on query above. 
     if(substr("$dirArray[$index]", 0, 1)!=$hide) { 

    // Resets Variables 
     $favicon=""; 
     $class="file"; 

    // Gets File Names 
     $name=$dirArray[$index]; 
     $namehref=$dirArray[$index]; 

     // Gets Date Modified 
     $modtime=date("M j Y g:i A", filemtime($dirArray[$index])); 
     $timekey=date("YmdHis", filemtime($dirArray[$index])); 


    // Separates directories, and performs operations on those directories 
     if(is_dir($dirArray[$index])) 
     { 
       $extn="&lt;Directory&gt;"; 
       $size="&lt;Directory&gt;"; 
       $sizekey="0"; 
       $class="dir"; 

      // Gets favicon.ico, and displays it, only if it exists. 
       if(file_exists("$namehref/favicon.ico")) 
        { 
         $favicon=" style='background-image:url($namehref/favicon.ico);'"; 
         $extn="&lt;Website&gt;"; 
        } 

      // Cleans up . and .. directories 
       if($name=="."){$name=". (Current Directory)"; $extn="&lt;System Dir&gt;"; $favicon=" style='background-image:url($namehref/.favicon.ico);'";} 
       if($name==".."){$name=".. (Parent Directory)"; $extn="&lt;System Dir&gt;";} 
     } 

    // File-only operations 
     else{ 
      // Gets file extension 
      $extn=pathinfo($dirArray[$index], PATHINFO_EXTENSION); 

      // Prettifies file type 
      switch ($extn){ 
       case "png": $extn="PNG Image"; break; 
       case "jpg": $extn="JPEG Image"; break; 
       case "jpeg": $extn="JPEG Image"; break; 
       case "svg": $extn="SVG Image"; break; 
       case "gif": $extn="GIF Image"; break; 
       case "ico": $extn="Windows Icon"; break; 

       case "txt": $extn="Text File"; break; 
       case "log": $extn="Log File"; break; 
       case "htm": $extn="HTML File"; break; 
       case "html": $extn="HTML File"; break; 
       case "xhtml": $extn="HTML File"; break; 
       case "shtml": $extn="HTML File"; break; 
       case "php": $extn="PHP Script"; break; 
       case "js": $extn="Javascript File"; break; 
       case "css": $extn="Stylesheet"; break; 

       case "pdf": $extn="PDF Document"; break; 
       case "xls": $extn="Spreadsheet"; break; 
       case "xlsx": $extn="Spreadsheet"; break; 
       case "doc": $extn="Microsoft Word Document"; break; 
       case "docx": $extn="Microsoft Word Document"; break; 

       case "zip": $extn="ZIP Archive"; break; 
       case "htaccess": $extn="Apache Config File"; break; 
       case "exe": $extn="Windows Executable"; break; 

       default: if($extn!=""){$extn=strtoupper($extn)." File";} else{$extn="Unknown";} break; 
      } 

      // Gets and cleans up file size 
       $size=pretty_filesize($dirArray[$index]); 
       $sizekey=filesize($dirArray[$index]); 
     } 

    // Output 
    echo(" 
     <tr class='$class'> 
      <td><a href='./$namehref'$favicon class='name'>$name</a></td> 
      <td><a href='./$namehref'>$extn</a></td> 
      <td sorttable_customkey='$sizekey'><a href='./$namehref'>$size</a></td> 
      <td sorttable_customkey='$timekey'><a href='./$namehref'>$modtime</a></td> 
     </tr>"); 
     } 
    } 
    ?> 

     </tbody> 
    </table> 
+0

那麼filemtime()如何失敗?它返回的時間不正確嗎?它是拋出一個異常還是顯示一個錯誤,它是否會留下空白頁? –

+1

如果您收到'stat failed',那麼您使用的文件名/路徑無效,或者您無權訪問文件所在的一個或多個父目錄。 –

+1

權限設置爲全部打開,因此是整個安全網站的一部分,3人可以訪問它。它返回此 1432811000.jpg JPEG圖像字節Jan 1 1970 12:00 AM 並單擊時,不會將您帶到圖像。這是遺漏了上傳/部分的網址 – vcurams91

回答

1

試着用這個。 dirArray的路徑沒有通過。需要追加$路徑到arraypath

<form enctype="multipart/form-data" action="./.upload_file.php" method="post"> 
       <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> 
       Choose a file to upload: <input name="uploaded_file" type="file" /> 
       <input type="submit" value="Upload" /> 
       </form> 

       <table class="sortable"> 
        <thead> 
        <tr> 
         <th>Filename</th> 
         <th>Type</th> 
         <th>Size</th> 
         <th>Date Modified</th> 
         <th>Delete File?</th> 
        </tr> 
        </thead> 
        <tbody> 
        <?php 


       // Adds pretty filesizes 
       function pretty_filesize($file) { 
        $size = filesize($file); 
        if($size < 1024){$size = $size." Bytes";} 
        elseif(($size<1048576)&&($size>1023)){$size=round($size/1024, 1)." KB";} 
        elseif(($size<1073741824)&&($size>1048575)){$size=round($size/1048576, 1)." MB";} 
        else{$size=round($size/1073741824, 1)." GB";} 
        return $size; 
       } 



       // Checks to see if veiwing hidden files is enabled 
       if($_SERVER['QUERY_STRING']=="hidden") 
       {$hide=""; 
       $ahref="./"; 
       $atext="Hide";} 
       else 
       {$hide="."; 
       $ahref="./?hidden"; 
       $atext="Show";} 



       // Opens directory 
       $path = "./uploads/"; 
       //$myDirectory=opendir("$path"); 
       $myDirectory=opendir('./uploads/'); 

       // Gets each entry 
       while($entryName=readdir($myDirectory)) { 
        $dirArray[]=$entryName; 
       } 
       //var_dump($dirArray); 
       // Closes directory 
       closedir($myDirectory); 

       // Counts elements in array 
       $indexCount=count($dirArray); 


       // Sorts files 
       sort($dirArray); 

       // Loops through the array of files 
       for($index=0; $index < $indexCount; $index++) { 


       // Decides if hidden files should be displayed, based on query above. 
        if(substr("$dirArray[$index]", 0, 1)!=$hide) { 

       // Resets Variables 
        $favicon=""; 
        $class="file"; 

       // Gets File Names 
        $name = $path.$dirArray[$index]; 
        $namehref = $path.$dirArray[$index]; 

        // Gets Date Modified 
        $modtime=date("M j Y g:i A", filemtime($path.$dirArray[$index])); 
        $timekey=date("YmdHis", filemtime($path.$dirArray[$index])); 


       // Separates directories, and performs operations on those directories 
        if(is_dir($dirArray[$index])) 
        { 
          $extn="&lt;Directory&gt;"; 
          $size="&lt;Directory&gt;"; 
          $sizekey="0"; 
          $class="dir"; 

         // Gets favicon.ico, and displays it, only if it exists. 
          if(file_exists("$namehref/favicon.ico")) 
           { 
            $favicon=" style='background-image:url($namehref/favicon.ico);'"; 
            $extn="&lt;Website&gt;"; 
           } 

         // Cleans up . and .. directories 
          if($name=="."){$name=". (Current Directory)"; $extn="&lt;System Dir&gt;"; $favicon=" style='background-image:url($namehref/.favicon.ico);'";} 
          if($name==".."){$name=".. (Parent Directory)"; $extn="&lt;System Dir&gt;";} 
        } 

       // File-only operations 
        else{ 
         // Gets file extension 
         $extn=pathinfo($dirArray[$index], PATHINFO_EXTENSION); 

         // Prettifies file type 
         switch ($extn){ 
          case "png": $extn="PNG Image"; break; 
          case "jpg": $extn="JPEG Image"; break; 
          case "jpeg": $extn="JPEG Image"; break; 
          case "svg": $extn="SVG Image"; break; 
          case "gif": $extn="GIF Image"; break; 
          case "ico": $extn="Windows Icon"; break; 

          case "txt": $extn="Text File"; break; 
          case "log": $extn="Log File"; break; 
          case "htm": $extn="HTML File"; break; 
          case "html": $extn="HTML File"; break; 
          case "xhtml": $extn="HTML File"; break; 
          case "shtml": $extn="HTML File"; break; 
          case "php": $extn="PHP Script"; break; 
          case "js": $extn="Javascript File"; break; 
          case "css": $extn="Stylesheet"; break; 

          case "pdf": $extn="PDF Document"; break; 
          case "xls": $extn="Spreadsheet"; break; 
          case "xlsx": $extn="Spreadsheet"; break; 
          case "doc": $extn="Microsoft Word Document"; break; 
          case "docx": $extn="Microsoft Word Document"; break; 

          case "zip": $extn="ZIP Archive"; break; 
          case "htaccess": $extn="Apache Config File"; break; 
          case "exe": $extn="Windows Executable"; break; 

          default: if($extn!=""){$extn=strtoupper($extn)." File";} else{$extn="Unknown";} break; 
         } 

         // Gets and cleans up file size 
          $size=pretty_filesize($path.$dirArray[$index]); 
          $sizekey=filesize($path.$dirArray[$index]); 
        } 
        $varlocation = $name; 
        //echo 'is the table being displayed?'; 
       // Output 
       echo(" 
        <tr class='$class'> 
         <td><a href='./$namehref'$favicon class='name'>$name</a></td> 
         <td><a href='./$namehref'>$extn</a></td> 
         <td sorttable_customkey='$sizekey'><a href='./$namehref'>$size</a></td> 
         <td sorttable_customkey='$timekey'><a href='./$namehref'>$modtime</a></td> 
         <td><i class='fa fa-trash-o'></i>".$varlocation." </td> 
        </tr>"); 
        } 
       } 
       ?> 

        </tbody> 
    </table>