Pb与ssh2_scp_send SFTP

时间:2013-01-08 18:39:25

标签: php sftp

我想上传文件并使用SFTP保存。

这是我的代码:

if ($size != 0) {
            $name = $uploadedFile->getClientOriginalName();
            $extension = $uploadedFile->getClientMimeType();
            $extension = \explode('.', $name);
            $shortName = $extension[0];
            $extension = \end($extension);
            $allowedExtension = "zip";
            $compare = strcmp(strtolower($extension), $allowedExtension);            
            if ($compare == 0) {//les deux chaines sont égaux
                $saveName = $name;                 
                if (file_exists($target_path . $name))
                    $saveName = $shortName . $date . '.' . $extension;
                /* modif le dossier sera déposé en SFTP*/

                /* Connexion SFTP */
                $connection = ssh2_connect('webhost01', 22); // acces inetrne
                ssh2_auth_password($connection, 'fbh', 'F#Bh52.6');
                $sftp = ssh2_sftp($connection);
                /* Déplacer le dossier dans le sftp */
                ssh2_scp_send($connection,$saveName, '/home/fbh/newdir/', 0677);
}

但是我收到此错误ssh2_scp_send(12100102430010813_192636.zip)[function.ssh2-scp-send]:无法打开流:没有这样的文件或目录。任何帮助PLZ

2 个答案:

答案 0 :(得分:0)

您修改了$ saveName但未将文件移动到此位置。使用file_exists($ saveName)

进行检查

答案 1 :(得分:0)

感谢您的回答。现在好了,问题是我应该从服务器上的临时文件中撤回它。

这里是新代码:

if ($size != 0) {            
            $name = $uploadedFile['name'];            
            $extension = $uploadedFile['type'];
            $extension = \explode('.', $name);
            $shortName = $extension[0];
            $extension = \end($extension);
            $allowedExtension = "zip";
            $compare = strcmp(strtolower($extension), $allowedExtension);                          
            if ($compare == 0) {//les deux chaines sont égaux
                $tempFile= $uploadedFile['tmp_name'];
                $saveName = $name;                 
                if (file_exists($target_path . $name))
                    $saveName = $shortName . $date . '.' . $extension;
                /*
                 *  modif le dossier sera déposé en SFTP Cnx + deposer
                 */ 

                /*
                 * parametres SFTP
                 */
                $host = $this->container->getParameter('host');
                $port = $this->container->getParameter('port');
                $user = $this->container->getParameter('user');
                $mdp  = $this->container->getParameter('mdp');
                $pathSFTP  = $this->container->getParameter('pathSFTP');
                $connection = ssh2_connect($host, $port); // acces inetrne
                ssh2_auth_password($connection, $user, $mdp);
                $sftp = ssh2_sftp($connection);
                /* Déplacer le dossier dans le sftp */
                ssh2_scp_send($connection,$tempFile, '$pathSFTP'.newFile.zip, 0677);                
                $spi = $this->get('spi');
                $partenaireManager = $spi->getPartenaireManager();
                $id_import = $partenaireManager->insertImport($id, $saveName);

                if ($id_import > 0) {

                    return $this->render('ListBundle:Default:rapportUpload.html.twig', array('messgae' => $name . " a été uploadé avec succées",
                                'id_partenaire' => $id, 'id_import' => $id_import));
                }
            } else {
                return $this->render('ListBundle:Default:rapportUpload.html.twig', array('messgae' => "L'extention de " . $name . " est invalide",
                            'id_partenaire' => $id));
            }
        }