2013-03-12 48 views
0

所以我在PHP這個代碼,需要一個形式的結果:失敗通過PHP上傳圖像服務器

<?php if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) 
{ 
    $fileName = $_FILES['userfile']['name']; 
    $tmpName = $_FILES['userfile']['tmp_name']; 
    $fileSize = $_FILES['userfile']['size']; 
    $fileType = $_FILES['userfile']['type']; 

    $path = $_SERVER['DOCUMENT_ROOT'] . "/uploads/"; 
    //$path = "uploads/"; 
    $path = $path . basename($_FILES['userfile']['name']); 

    is_uploaded_file($_FILES['userfile']['tmp_name']) or error('not an HTTP upload'); 

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $path)) 
    { 
     echo "The file ". basename($_FILES['userfile']['name']) . " has been uploaded, and your information has been added to the directory"; 
    } 
    else 
    { 
     echo "Sorry, there was a problem uploading your file -->" . $path; 
    } 


    } ?> 

我的問題是,代碼經過is_uploaded_file但在執行move_uploaded_file

時失敗

此代碼是我在許多php論壇和教程中找到的代碼。

任何人都知道爲什麼它不會上傳到我的服務器上。

編輯:

下面是用HTML代碼完整PHP文件:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Montréal Accessible</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
    <meta charset="UTF-8"> 
    <link rel="stylesheet" type="text/css" href="vendors/bootstrap/css/bootstrap.min.css"/> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css"> 
    <link rel="stylesheet" type="text/css" href="stylesheets/home.css"/> 
    <link rel="stylesheet" type="text/css" href="stylesheets/grid.css"> 
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD9wqmeX_YlaRLZYrnNDOkHcXvlXa9cATo&sensor=false"></script> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script> 
    <script type="text/javascript" src="scripts/home.js"></script> 

    <?php if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) 
    { 
     $fileName = $_FILES['userfile']['name']; 
     $tmpName = $_FILES['userfile']['tmp_name']; 
     $fileSize = $_FILES['userfile']['size']; 
     $fileType = $_FILES['userfile']['type']; 

     $path = $_SERVER['DOCUMENT_ROOT'] . "/uploads/"; 
     //$path = "uploads/"; 
     $path = $path . basename($_FILES['userfile']['name']); 

     is_uploaded_file($_FILES['userfile']['tmp_name']) or error('not an HTTP upload'); 

     if (move_uploaded_file($_FILES['userfile']['tmp_name'], $path)) 
     { 
      echo "The file ". basename($_FILES['userfile']['name']) . " has been uploaded, and your information has been added to the directory"; 
     } 
     else 
     { 
      echo "Sorry, there was a problem uploading your file -->" . $path; 
     } 

     $myvars = ''; 
     $ch = curl_init($url); 
     curl_setopt($ch, CURLOPT_POST, 1); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
     curl_setopt($ch, CURLOPT_HEADER, 0); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

     $upImgResponse = curl_exec($ch);*/ 
    } ?> 

    <script type="text/javascript"> 
     window.onload = function() 
     { 
      loginForm = document.forms[0]; 
      loginForm.onsubmit=function() 
      { 
       var name = prompt ("Enter your name","") 

       if(loginForm[0].value == '') 
       { 
        alert('Please enter a username'); 
        return false; 
       } 
       else 
       { 
        loginForm.action='../Symfony/web/app.php/login/' + name + '/' + ...; 
        return true; 
       } 
      } 
     } 
    </script> 

</head> 
<body> 
    <div id="container"> 
     <div id="headerBar"> 
      <div id="homeLogoColumn"> 
       <div id="homeLogo">Montréal Accessible</div> 
      </div> 
      <div id="searchBarColumn"> 
       <div id="searchBar" class="input-append"> 
        <input id="appendedInputButton" type="text"> 
        <a id="searchButton" class="btn btn-primary" href="#"><i class="icon-search icon-white"></i></a> 
       </div> 
      </div> 
      <div id="anchorsColumn"> 
         <a class="lastAnchor" href="#">À propos</a> 
         <a href="#">Contact</a> 
         <a href="#">Aide</a> 
         <!--<form name="input" action="" method="post"> 
         <a href="#">Connexion</a><input type="submit" value="Connect"> 
         </form> --> 

      </div> 
     </div> 


     <form method="post" enctype="multipart/form-data"> 
     <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> 
     <tr> 
     <td width="246"> 
     <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> 
     <input name="userfile" type="file" id="userfile"> 
     </td> 
     <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> 
     </tr> 
     </table> 
     </form> 

     <div id="mainContent"> 
       <div id="paramsAndResultsColumn"> 
        <div id="paramsAndResultsTabs"> 
         <ul> 
         <li><a href="#paramsTab"><span>Critères</span></a></li> 
         <li><a href="#resultsTab"><span>Résultats</span></a></li> 
         </ul> 
         <div id="paramsTab"></div> 
         <div id="resultsTab"></div> 
        </div> 
       </div> 
       <div id="mapCanvasColumn"> 
        <div id="mapCanvas"></div> 
       </div> 
     </div> 
    </div> 
</body> 
</html> 

編輯2:

所顯示的錯誤是:「對不起,出現了一個問題,上載。 ...「意思是它通過其他的...

+1

是$ path可寫?你是老闆? – 2013-03-12 19:17:19

+0

是的,我是所有者,它是可寫的。 – nevero 2013-03-12 19:18:16

+0

發佈您的表單代碼。 – 2013-03-12 19:19:28

回答

0

感謝您的幫助...問題是一個權限問題。我使用chmod 755而不是chmod設置文件夾的權限777

+0

默認情況下,至少在我的服務器上,所有文件夾都設置爲755.當事情不起作用時,那就是當你可以chmod到777更糟糕的情況。很高興爲你工作。 – 2013-03-12 19:50:41

0

使用此' 0) { $ fileN ame = $ _FILES ['userfile'] ['name']; $ tmpName = $ _FILES ['userfile'] ['tmp_name']; $ fileSize = $ _FILES ['userfile'] ['size']; $ fileType = $ _FILES ['userfile'] ['type']; echo $ tmpName;

$path = "uploads/"; 
    $path = $path . basename($_FILES['userfile']['name']); 

    is_uploaded_file($_FILES['userfile']['tmp_name']) or error('not an HTTP upload'); 

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $path)) 
    { 
     echo "The file ". basename($_FILES['userfile']['name']) . " has been uploaded, and your information has been added to the directory"; 
    } 
    else 
    { 
     echo "Sorry, there was a problem uploading your file -->" . $path; 
    } 

    /*$url="http://rapliq.lerb.polymtl.ca/Symfony/web/app.php/upimg/" . $fileName . "/" . $path . "/"; 

    $myvars = ''; 
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    $upImgResponse = curl_exec($ch);*/ 
} ?>` 

這個問題是與你沒有給主要根源的洗澡。另外確保文件夾可以訪問。

+0

不幸的是,它並沒有解決它。我實際上是在使用根之前使用這種方法...因爲我通過「絕對路徑」會更好 – nevero 2013-03-12 19:41:28