2017-05-20 3 views
2

Ich habe versucht, hochgeladene Bilder in einem Ordner mit PHP zu speichern. Ich bin daran festgefahren, weil der Name des Bildes anders als der Ordner in der Datenbank speichert.So speichern Sie hochgeladene Bild in Datei mit PHP

Datenbank: assets/images/profile_pics/john_doe5a153efc8731359393775e3355df0b77n.jpg

Folder: john_doe_original.5a153efc8731359393775e3355df0b77njpeg

include("includes/header2.php"); 

    $profile_id = $user['username']; 
    $imgSrc = ""; 
    $result_path = ""; 
    $msg = ""; 

    /*********************************************************** 
     0 - Remove The Temp image if it exists 
    ***********************************************************/ 
     if (!isset($_POST['x']) && !isset($_FILES['image']['name'])){ 
      //Delete users temp image 
       $temppath = 'assets/images/profile_pics/'.$profile_id.'_temp.jpeg'; 
       if (file_exists ($temppath)){ @unlink($temppath); } 
     } 


    if(isset($_FILES['image']['name'])){  
    /*********************************************************** 
     1 - Upload Original Image To Server 
    ***********************************************************/  
     //Get Name | Size | Temp Location   
      $ImageName = $_FILES['image']['name']; 
      $ImageSize = $_FILES['image']['size']; 
      $ImageTempName = $_FILES['image']['tmp_name']; 

     //Get File Ext 
      $ImageType = @explode('/', $_FILES['image']['type']); 
      $type = $ImageType[1]; //file type 
     //Set Upload directory  
      $uploaddir = $_SERVER['DOCUMENT_ROOT'].'/name/assets/images/profile_pics'; 
     //Set File name 
      $file_temp_name = $profile_id.'_original.'.md5(time()).'n'.$type; //the temp file name 
      $fullpath = $uploaddir."/".$file_temp_name; // the temp file path 
      $file_name = $profile_id.'_temp.jpeg'; //$profile_id.'_temp.'.$type; // for the final resized image 
      $finalname = $profile_id.md5(time()); 
      $fullpath_2 = "assets/images/profile_pics/".$finalname."n.jpg"; //for the final resized image 
     //Move the file to correct location 
      $move = move_uploaded_file($ImageTempName,$fullpath) ; 
      chmod($fullpath, 0777); 
      //Check for valid uplaod 
      if (!$move) { 
       die ('File didnt upload'); 
      } else { 
       $imgSrc= "assets/images/profile_pics/".$file_name; // the image to display in crop area 
       $msg= "Upload Complete!"; //message to page 
       $src = $file_name;   //the file name to post from cropping form to the resize   
      } 
     } 


    if (isset($_POST['Submit'])){ 


      //Insert image into database 
      $insert_pic_query = mysqli_query($con, "UPDATE users SET profile_pic='$fullpath_2' WHERE username='$userLoggedIn'"); 

      //header("Location: account.php"); 

    } 

Dank für Ihre Hilfe danken. Lassen Sie mich wissen, wie ich meine Frage verbessern kann.

Antwort

0

Verwenden Sie in Ihrer move_uploaded_file()$fullpath_2. ändern Upload directiry zu $uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/Halpper/';

if (isset($_FILES['image']['name'])) { 
    /*********************************************************** 
    * 1 - Upload Original Image To Server 
    ***********************************************************/ 
    //Get Name | Size | Temp Location 
    $ImageName = $_FILES['image']['name']; 
    $ImageSize = $_FILES['image']['size']; 
    $ImageTempName = $_FILES['image']['tmp_name']; 

    //Get File Ext 
    $ImageType = @explode('/', $_FILES['image']['type']); 
    $type = $ImageType[1]; //file type 
    //Set Upload directory 
    $uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/Halpper/'; 
    //Set File name 
    $file_temp_name = $profile_id . '_original.' . md5(time()) . 'n' . $type; //the temp file name 
    $fullpath = $uploaddir . "/" . $file_temp_name; // the temp file path 
    $file_name = $profile_id . '_temp.jpeg'; //$profile_id.'_temp.'.$type; // for the final resized image 
    $finalname = $profile_id . md5(time()); 
    $fullpath_2 = "assets/images/profile_pics/" . $finalname . "n.jpg"; //for the final resized image 
    //Move the file to correct location 
    if (move_uploaded_file($ImageTempName, $uploaddir . $fullpath_2)) { 
     chmod($uploaddir . $fullpath_2, 0777); 
    } 
    //Check for valid uplaod 
    if (!$move) { 
     die ('File didnt upload'); 
    } else { 
     $imgSrc = "assets/images/profile_pics/" . $file_name; // the image to display in crop area 
     $msg = "Upload Complete!"; //message to page 
     $src = $file_name;   //the file name to post from cropping form to the resize 
    } 
} 
+0

Ich erhalte einen Fehler (Warnung: chmod(): Keine solche Datei oder das Verzeichnis) in Zeile 44 nach fullpath_2 $ hinzufügen. Wie kann ich das beheben? Danke für Ihre Hilfe. –

+0

haben Sie den aktualisierten Code über – julekgwa

+0

kopiert Ja, ich habe den $ move zu dem von Ihnen empfohlenen geändert. Edit: Es hat funktioniert. –

Verwandte Themen