0

我想上传文件并使用 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]: failed to open stream: No such file or directory。任何帮助请

4

2 回答 2

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));
            }
        } 
于 2013-01-09T11:03:59.367 回答
0

您修改了 $saveName 但没有将文件移动到此位置。用 file_exists($saveName) 检查这个

于 2013-01-09T09:29:27.027 回答