2015-10-15 1 views
0

J'essaie de stocker mon nom d'image dans la base de données et l'image dans le dossier. Les images bougent mais le problème est que les images ne sont pas stockées avec le nom d'origine. Un nom de temp (image) préfixant avec mon nom d'image.Impossible de déplacer le fichier téléchargé avec le nom d'origine

Mon code:

//This is the directory where images will be saved 
$target = 'image/image'; 
$target = $target . basename($_FILES['photo']['name']); 

//This gets all the other information from the form 
$pic=($_FILES['photo']['name']); 
$query1 = mysql_query("SELECT * from image WHERE image_name = '$pic' "); 

if(!$query1) { 
    echo "error"; 
} 

$rows = mysql_num_rows($query1); 
if ($rows != 1) { 
    //Writes the information to the database 
    $query = mysql_query("INSERT INTO image(image_name) VALUES ('$pic')"); 

    //Writes the photo to the server 
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { 
     //Tells you if its all ok 
     echo "The file has been uploaded, and your information has been added to the directory"; 
    } 
    else { 
     //Gives and error if its not 
     echo "Sorry, there was a problem uploading your file."; 
    } 
} 
else { 
    echo "image already exits in database"; 
} 

Comment résoudre ce problème. Une idée?

+1

Essayez $ target = 'image/image /'; Je pense qu'il vous manque a/ – foxbeefly

+0

aucune erreur face à: fichier_place_uploaded (image/image/Error.png): impossible d'ouvrir le flux: aucun fichier ou répertoire – Nayana

+2

@foxbeefly est correct, vous devez ajouter un séparateur de répertoire ... Sur une note de côté, vous devez insérer dans votre base de données seulement si la file_move a réussi, sinon vous verrez des images qui n'existent pas – Salketer

Répondre

0

essayer ce code

$target= 'image/image'; 
$imagename = $_FILES['photo']['name']; 
$target = $target.$imagename; 
$temp = $_FILES['photo']['tmp_name'] 
echo '<pre>'; 
if (move_uploaded_file($temp, $target)) { 
    echo "File is successfully uploaded.\n"; 
} else { 
    echo "Possible file upload attack!\n"; 
} 
0
<?php 

    /* This is the directory where images will be saved */ 
    $target = 'image/image'; 
    /* Full target path for uploaded image */ 
    $target = $target . DIRECTORY_SEPARATOR . basename($_FILES['photo']['name']); 



    /* This gets all the other information from the form */ 
    $pic=$_FILES['photo']['name']; 
    $query1 = mysql_query("SELECT * from `image` WHERE `image_name` = '$pic';"); 



    if(!$query1) { 

     echo "error querying database"; 

    } else { 

     $rows = mysql_num_rows($query1); 

     /* If there are no records in db for this piture, add a record */ 
     if ($rows == 0) { 

      /* Writes the information to the database */ 
      $query = mysql_query("INSERT INTO `image` (`image_name`) VALUES ('$pic');"); 

      /* Writes the photo to the server */ 
      if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { 

       /* Tells you if its all ok */ 
       echo "The file has been uploaded, and your information has been added to the directory"; 

      } else { 

       /* Gives and error if its not */ 
       echo "Sorry, there was a problem uploading your file."; 

      } 
     } else { 
      echo "image already exits in database"; 
     } 
    } 

?> 
0
$file = $_FILES['fileup']['name']; 
$tmp_name = $_FILES['fileup']['tmp_name']; 
$target_dir = "uploads/".basename($file); 
if($file!="") { 
    if(move_uploaded_file($tmp_name,$target_dir)) { 
     echo 'success'; 
    } else { 
     echo 'not success'; 
    } 
} 
1

code utilisé:

//This is the directory where images will be saved 
$target = 'image/image'; 
$target = $target . basename($_FILES['photo']['name']); 

Si vous utilisez 'image/image', puis les images seront stockées sous image/ répertoire avec le préfixe image et image_name ie, imageimage_name.jpg

Si vous faites face à une erreur lors de l'utilisation image/image/, s'il vous plaît vérifier si vous avez la structure de répertoire comme 'image/image/' ou seulement image/