2013-07-22 19 views
0

我试图将文件上传到FTP服务器。它被捕获在if语句中,该语句声明该目录必须是chmode 777,但是我已经手动chmode 77在ftp中的每个目录以及在代码中添加一个函数。使用PHP将文件上传到FTP - 不会写入服务器

其次,我想问一下在上传过程开始后创建的.tmp文件。他们上传到FTP?如果是这样,我是否也需要允许访问.tmp文件以及.mp3文件?我真的不知道他们在这个过程中的用途是什么,所以如果有人可以花时间解释它,那会非常有帮助。

一些变量内容:

$upload_path = /htdocs/site2/telemessages/en/Apologies 
$_FILES['userfile']['tmp_name'] = /tmp/phpwkfxrW 

这里是我使用的传递文件到FTP代码:

$ftp_server="***********"; 
$ftp_user_name="************"; 
$ftp_user_pass="***********"; 

// set up basic connection 
$conn_id = ftp_connect($ftp_server); 

// login with username and password 
if(ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)){ 

// Configuration - Your Options 
     $allowed_filetypes = array('.mp3','.tmp'); // These will be the types of file that will pass the validation. 
     $max_filesize = 2097152; // Maximum filesize in BYTES (currently 2MB). 

     $cutName = substr($_SESSION['dir'], 0, -1); 

     $upload_path = "htdocs/site/telemessages/en/". $_SESSION['dir']; 
     echo "upload path: ".$upload_path; // The place the files will be uploaded to (currently a 'files' directory). 


    $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename. 

    // Check if the filetype is allowed, if not DIE and inform the user. 
    if(!in_array($ext,$allowed_filetypes)) 
     die('The file you attempted to upload is not allowed.'); 

    // Now check the filesize, if it is too large then DIE and inform the user. 
    if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) 
     die('The file you attempted to upload is too large.'); 

    // Check if we can upload to the specified path, if not DIE and inform the user. 
    if(!is_writable($upload_path)) 
     die('You cannot upload to the specified directory, please CHMOD it to 777.'); 

    // We'll start handling the upload in the next step 
    echo"FORCE: ".$_FILES['userfile']['tmp_name']; 
     //Upload the file to your specified path. 
    $result = move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename); 


    if(!$result) 
       { 
        return -1; 
       } 
       else 
       { 
        echo $result; 
        //SET PROPER READ PERMISSIONS 

        $result2 = chmod($upload_path, 0777); 
        echo "Result: ".$result2; 
        } 

}else{ 
echo "login failed"; 
} 

感谢提前任何帮助。

+1

$ upload_path =“htdocs/site/telemessages/en /”。 $ _SESSION [ 'DIR']; - 这应该有一个领先的“/”? –

回答

0

乍一看,有一些事情可能会导致你的麻烦。

$upload_path = /htdocs/site2/telemessages/en/Apologies 

我打算假设在代码中实际上有引号。如果不是,那需要修复。

可能性:

  1. Web服务器无法访问该目录。我知道一些服务器场在多台服务器上使用NFS,并且可能会出现奇怪的许可事件。你可能想检查一下你的服务器公司。
  2. /htdocs/site2/telemessages/en/Apologies实际上可能是文件而不是目录,这可能会导致问题。
  3. Paul Bailey上面的评论:您可能试图从系统根目录中引用一个路径,当它离开应用程序根目录时。因此他对领导/的评论。