FILEUPLOAD [SYMFONY] -> Could not create target directory

Par Alain Ouakbar Tabba Sardinaluile, il y a 9 ans


Bonjour, bonsoir,

J'ai écrit une méthode pour uploader des fichiers. En local, ca marche super.

Une fois installé sur le serveur (OVH), premier plantage : Could not create target directory to move temporary file into.
DONC : je créé le directory (uploads) Et là, ca ne marche pas !! Je n'en peux plus...

voici le catch de mon erreur :

in /home/newopens/www/morin/src/File/FileBundle/Entity/UploadFileMover.php at line 46 -around $ret = mkdir($targetDir, umask(), true);around if (!$ret) {around throw new \RuntimeException("Could not create target directory to move temporary file into.");around }around } around $file->move($targetDir, basename($targetFilePath));

EN FAIT : IL FAUT QU'IL CREE UN REPERTOIRE UPLOADS DANS LE REPERTOIRE UPLOADS DU REPERTOIRE WEB.

ALORS : je créé le répertoire UPLOADS ET JE LE CHMOD à 777 (via FTP)

MEME ERREUR !

ce que je ne comprends pas c'est que ca marche en local et pas sur le serveur distant...

MERCI DE VOTRE AIDE

function UPLOADER de mon controller :

public function UploadAction(Request $request, $id_article = 1, $page = 1) { if ($request->getMethod() == 'POST') { $image = $request->files->get('img'); $status = 'success'; $uploadedURL = ''; $message = ''; if (($image instanceof UploadedFile) && ($image->getError() == '0')) { if (($image->getSize() < 20000000)) { $originalName = $image->getClientOriginalName(); $name_array = explode('.', $originalName); $file_type = $name_array[sizeof($name_array) - 1]; $valid_filetypes = array('jpg', 'jpeg', 'bmp', 'png'); if (in_array(strtolower($file_type), $valid_filetypes)) { $document = new Document(); $document->setFile($image); $document->setSubDirectory('uploads'); $document->processFile(); $uploadedURL = $document->getUploadDirectory() . DIRECTORY_SEPARATOR . $document->getSubDirectory() . DIRECTORY_SEPARATOR . $image->getBasename(); $nouveau_fic_250 = 'uploads/img/petite/' . rand(1, 99999999) . '.' . $file_type; $nouveau_fic_1000 = 'uploads/img/grande/' . rand(1, 99999999) . '.' . $file_type; copy($uploadedURL, $nouveau_fic_250); copy($uploadedURL, $nouveau_fic_1000); header('Content-Type: image/jpeg'); $grande_image = imagecreatetruecolor(1000, 1438); $petite_image = imagecreatetruecolor(250, 359); switch ($file_type) { case 'bmp': $source_grande = imagecreatefromwbmp($nouveau_fic_1000); $source_petite = imagecreatefromwbmp($nouveau_fic_250); break; case 'gif': $source_grande = imagecreatefromgif($nouveau_fic_1000); $source_petite = imagecreatefromgif($nouveau_fic_250); break; case 'jpg': $source_grande = imagecreatefromjpeg($nouveau_fic_1000); $source_petite = imagecreatefromjpeg($nouveau_fic_250); break; case 'png': $source_grande = imagecreatefrompng($nouveau_fic_1000); $source_petite = imagecreatefrompng($nouveau_fic_250); break; default : return "Unsupported picture type!"; } list($width, $height) = getimagesize($nouveau_fic_250); if (!imagecopyresampled($grande_image, $source_grande, 0, 0, 0, 0, 1000, 1438, $width, $height)) echo "erreur !!!!"; if (!imagecopyresampled($petite_image, $source_petite, 0, 0, 0, 0, 250, 359, $width, $height)) echo "erreur !!!!"; imagejpeg($grande_image, $nouveau_fic_1000, 100); imagejpeg($petite_image, $nouveau_fic_250, 100); $response = $this->forward('OCPlatformBundle:majarticles:modifierImageArticle', array('id_article' => $id_article, 'page' => $page, 'nouveauChemin' => $nouveau_fic_250, 'nouveauChemingrande' => $nouveau_fic_1000, 'rechargerpage' => 'oui')); return $response; // return $this->redirect($this->generateUrl('test_majarticle', array('id' => $id_article, 'page' => $page))); // return $this->redirect($this->generateUrl('modifierimagearticle', array('id_article' => $id_article, 'page' => $page, 'cheminimage' => $nouveau_fic))); } else { $status = 'failed'; $message = 'Invalid File Type'; } } else { $status = 'failed'; $message = 'Size exceeds limit'; } } else { $status = 'failed'; $message = 'File Error'; } return $this->render('FileBundle:Default:index.html.twig', array('status' => $status, 'message' => $message, 'uploadedURL' => $uploadedURL, 'id_article' => $id_article, 'page' => $page )); } else { return $this->render('FileBundle:Default:index.html.twig', array('id_article' => $id_article, 'page' => $page)); } }

ENTITY Document :

<?php namespace File\FileBundle\Entity; /** * Description of Document * * @author Manoj */ use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\UploadedFile; class Document { private $file; private $subDir; private $filePersistencePath; /** @var string */ protected static $uploadDirectory = '%kernel.root_dir%/../web/uploads'; static public function setUploadDirectory($dir) { self::$uploadDirectory = $dir; } static public function getUploadDirectory() { if (self::$uploadDirectory === null) { throw new \RuntimeException("Trying to access upload directory for profile files"); } return self::$uploadDirectory; } public function setSubDirectory($dir) { $this->subDir = $dir; } public function getSubDirectory() { if ($this->subDir === null) { throw new \RuntimeException("Trying to access sub directory for profile files"); } return $this->subDir; } public function setFile(File $file) { $this->file = $file; } public function getFile() { return new File(self::getUploadDirectory() . "/" . $this->filePersistencePath); } public function getOriginalFileName() { return $this->file->getClientOriginalName(); } public function getFilePersistencePath() { return $this->filePersistencePath; } public function processFile() { if (! ($this->file instanceof UploadedFile) ) { return false; } $uploadFileMover = new UploadFileMover(); $this->filePersistencePath = $uploadFileMover->moveUploadedFile($this->file, self::getUploadDirectory(),$this->subDir); } } ?>

CLASS UploadFileMover

<?php namespace File\FileBundle\Entity; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ use Symfony\Component\HttpFoundation\File\UploadedFile; /** * Description of UploadFileMover * * @author Manoj */ class UploadFileMover { public function moveUploadedFile(UploadedFile $file, $uploadBasePath,$relativePath) { $originalName = $file->getClientOriginalName(); $targetClientOriginalName = $relativePath . DIRECTORY_SEPARATOR . $originalName; $targetFilePath = $uploadBasePath . DIRECTORY_SEPARATOR . $targetClientOriginalName; $ext = $file->getExtension(); $i=1; while (file_exists($targetFilePath) && md5_file($file->getPath()) != md5_file($targetFilePath)) { if ($ext) { $prev = $i == 1 ? "" : $i; $targetFilePath = $targetFilePath . str_replace($prev . $ext, $i++ . $ext, $targetFilePath); } else { $targetFilePath = $targetFilePath . $i++; } } $targetDir = $uploadBasePath . DIRECTORY_SEPARATOR . $relativePath; if (!is_dir($targetDir)) { $ret = mkdir($targetDir, umask(), true); if (!$ret) { throw new \RuntimeException("Could not create target directory to move temporary file into."); } } $file->move($targetDir, basename($targetFilePath)); return str_replace($uploadBasePath . DIRECTORY_SEPARATOR, "", $targetFilePath); } } ?>

1 réponse

Alain Ouakbar Tabba Sardinaluile, il y a 9 ans

PRECISION :

en forçant $targetDir= "/home/newopens/www/morin/web/uploads/uploads";
alors, l'upload fonctionne mais j'ai une erreur "Internal Server Error"
Y'a du mieux.
C'est donc un problème de chemin...
Je continue à chercher...