简体   繁体   English

使用PHP将文件上传到FTP-不会写入服务器

[英]Uploading file to FTP using PHP - will not write to server

I'm trying to upload a file to an FTP server. 我正在尝试将文件上传到FTP服务器。 It gets caught in the if statement which states that the directory must be chmode 777 however I have manually chmode 77 every directory in the ftp as well as adding a function in the code. 它被if语句捕获,该语句指出该目录必须是chmode 777,但是我手动在ftp中的每个目录中手动chmode 77以及在代码中添加了一个功能。

Secondly, I'd like to ask about the .tmp files that are created once an upload process has begun. 其次,我想问一下上载过程开始后创建的.tmp文件。 Are they uploaded to the FTP? 他们上传到FTP了吗? If so, do I need to also allow access for .tmp files along with .mp3 files? 如果是这样,我是否还需要允许同时访问.tmp文件和.mp3文件? I'm pretty unaware of what their use in the process actually is so if anyone could take the time to explain it that'd be very helpful. 我几乎不知道他们在此过程中实际上有什么用,所以如果有人可以花时间解释它,那将非常有帮助。

some variable contents: 一些可变的内容:

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

Here is the code I'm using to pass the file to the FTP: 这是我用来将文件传递到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";
}

Thanks for any help in advance. 感谢您的任何帮助。

At first glance, there are a couple of things that could be causing your trouble. 乍看之下,有几件事可能会引起您的麻烦。

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

I'm going to assume that actually has quote marks around it in your code. 我将假设您的代码中实际上在其周围有引号。 If not, that needs to be fixed. 如果不是,则需要解决。

Possibilities: 可能性:

  1. The web server doesn't have access to that directory. Web服务器无权访问该目录。 I know that some server farms use NFS across multiple servers and there can be weird permission things that happen. 我知道某些服务器场在多台服务器上使用NFS ,并且可能发生奇怪的权限事件。 You might want to check with your server company. 您可能需要与服务器公司联系。
  2. It is possible that /htdocs/site2/telemessages/en/Apologies is actually a file and not a directory, which could cause an issue. /htdocs/site2/telemessages/en/Apologies可能实际上是文件而不是目录,这可能会引起问题。
  3. Paul Bailey's comment above: You might be trying to reference a path off the system root directory, when it should be off the application root directory. 保罗·贝利(Paul Bailey)的评论:您可能试图引用系统根目录之外的路径,而该路径应位于应用程序根目录之外。 Hence his comment about the leading / . 因此,他对领导/评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM