2017-01-02 3 views
1

Ich mache eine Website. Ich mache es so, wenn du ein neues Profilbild hochlädst, wird es in die Benutzerkennung umbenannt. wie mache ich das? Es ist für Profilbilder. So kann ich den Benutzer Bilder und Uploads besser funktionieren als sie es getan haben. der Code:Benennen Sie eine Datei in eine ID

<?php 
require_once('connection.php'); 
require_once('auth.php'); 
$id=$_SESSION['SESS_MEMBER_ID']; 
$target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$temp = explode($_FILES["file"]["name"]); 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
// Check if image file is a actual image or fake image 
if(isset($_POST["submit"])) { 
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
    if($check !== false) { 
     echo "File is an image - " . $check["mime"] . "."; 
     $uploadOk = 1; 
    } else { 
     echo "File is not an image."; 
     $uploadOk = 0; 
    } 
} 
// Check if file already exists 
if (file_exists($target_file)) { 
    echo "Sorry, file already exists."; 
    $uploadOk = 0; 
} 
// Check file size 
if ($_FILES["fileToUpload"]["size"] > 500000) { 
    echo "Sorry, your file is too large."; 
    $uploadOk = 0; 
} 
// Allow certain file formats 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
&& $imageFileType != "gif") { 
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
    $uploadOk = 0; 
} 
// Check if $uploadOk is set to 0 by an error 
if ($uploadOk == 0) { 
    echo "Sorry, your file was not uploaded."; 
// if everything is ok, try to upload file 
} else { 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file . $id.".".$temp)) { 
     echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
    } else { 
     echo "Sorry, there was an error uploading your file."; 
    } 
} 
?> 
<?php 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$sql = "UPDATE users SET picture='uploads/$id' WHERE mem_id='$id'"; 

if ($conn->query($sql) === TRUE) { 
    echo "Record updated successfully"; 
} else { 
    echo "Error updating record: " . $conn->error; 
} 

$conn->close(); 
?> 
+1

Sieht aus wie Sie bereits mit ' $ target_file. $ id. ".". $ temp'. Was funktioniert nicht? – ceejayoz

+0

sag ich lade eine Datei namens doggo.png hoch, benennt sie in doggo.png4array um – Moo06

+1

Weil '$ temp' Array ist! –

Antwort

0

Ich denke, die Art und Weise explodieren Ihre Verwendung ist nicht richtig:

$temp = explode(".", $_FILES["file"]["name"]); 

und dann wird die Dateierweiterung sein end($temp):

$newfilename = $id . '.' . end($temp); // e.g. "png" 

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir . $id.".".$newfilename) 
Verwandte Themen