2016-07-05 12 views
0

Ich füge "_thumb" zum Dateinamen hinzu, wenn das Optionsfeld "thumbnail" ausgewählt ist, und füge es nicht hinzu (lasse den Dateinamen wie er ist) wenn "full size " ist ausgewählt. Dies funktioniert für meine Datenbankeinträge, aber nicht für die eigentliche Datei. Ich vermute, dass ich irgendwie "_thumb" zu beiden Dateitypen hinzufüge - Thumbnail und Fullsize - weil ich in dem Ordner, in den ich hochgeladen habe, nur den mit "_thumb" hinzugefügt habe. Ich kann nicht herausfinden, was falsch ist, bitte helfen!PHP Dateiname beim Hochladen für Miniaturansichten ändern

Das ist mein php so weit:

<?php 
 
session_start(); 
 
if(isset($_POST['category'])) { 
 
$_SESSION['category']=$_POST['category']; } 
 
if(isset($_POST['size'])) { 
 
$_SESSION['size']=$_POST['size']; } 
 
?> 
 
<!doctype html> 
 
<html> 
 
<head> 
 
<meta charset="UTF-8"> 
 
<title>Upload</title> 
 
<style>.thumbnails{height:60px;display:block;}.galery{height:200px;}</style> 
 
</head> 
 

 
<body> 
 

 
<?php 
 

 
$con = mysqli_connect("localhost","Melvin","") or die ("could not connect to server: " . mysqli_connect_error($con)); 
 
mysqli_select_db($con, "galerie") or die ("Could not connect to database: " . mysqli_error($con)); 
 

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

 
$name = $_FILES['file']['name']; 
 
$tmp_name = $_FILES['file']['tmp_name']; 
 
$location = '_images/_galerie/'; 
 
$target = '_images/_galerie/' .$name; 
 

 
\t if(move_uploaded_file($tmp_name,$location.$name)){ 
 
\t \t 
 
\t \t echo "Successfully uploaded"; 
 
\t \t 
 
\t \t $nam = $_POST['nam']; 
 
\t \t $category = $_POST['category']; 
 
\t \t $size = $_POST['size']; 
 
\t \t 
 
\t \t if ($size == 'thumb') { 
 
\t \t \t // add "thumb" between filename and extension \t \t \t 
 
\t \t \t $extension_pos = strrpos($target, '.'); // find position of the last dot, so where the extension starts 
 
\t \t \t $thumb = substr($target, 0, $extension_pos) . '_thumb' . substr($target, $extension_pos); \t \t \t 
 
\t \t \t $query = mysqli_query($con , "INSERT INTO images(img_name,img_title,img_cat,img_size)VALUES('".$thumb."','$nam','$category','$size')"); \t 
 
\t \t } else { 
 
\t \t \t $query = mysqli_query($con , "INSERT INTO images(img_name,img_title,img_cat,img_size)VALUES('".$target."','$nam','$category','$size')"); \t 
 
\t \t } \t \t 
 
\t \t 
 
\t \t function renameImg() { 
 
\t \t \t $name = $_FILES['file']['name']; 
 
\t \t \t $target = '_images/_galerie/' .$name; 
 
\t \t \t $extension_pos = strrpos($target, '.'); 
 
\t \t \t $thumb = substr($target, 0, $extension_pos) . '_thumb' . substr($target, $extension_pos); 
 
\t \t \t rename($target, $thumb); 
 
\t \t \t //echo $name . " replaced with " . $thumb; 
 
\t \t }; 
 
\t \t renameImg(); 
 
\t \t 
 
\t } else { 
 
\t \t 
 
\t \t echo "file not uploaded"; 
 
\t \t \t 
 
\t } 
 

 
} 
 
?> 
 

 
<div style="margin:20px 0 40px 0;"> 
 
    <form action="stack_overflow_upload_2.php" method="POST" enctype="multipart/form-data">  
 
     Upload: <input type="file" name="file"> 
 
     Title: <input type="text" name="nam" value="Image Gallery"> 
 
     Category: <select name="category" id="selectCat"> 
 
    
 
      <option value="black" 
 
\t \t \t <?php 
 
\t \t \t if (isset($_SESSION['category'])) { 
 
\t \t \t \t if($_SESSION['category'] == "black"){ 
 
\t \t \t \t \t echo ' selected'; }} 
 
\t \t \t ?> >black</option> 
 
      <option value="colour" 
 
\t \t \t <?php 
 
      if (isset($_SESSION['category'])) { 
 
\t \t \t \t if($_SESSION['category'] == "colour"){ 
 
\t \t \t \t \t echo ' selected'; }} 
 
\t \t \t ?> >colour</option> 
 
     </select> 
 
     
 
     \t <br>   
 
     \t 
 
     <input type="radio" name="size" value="full" id="regularRadio"   
 
     <?php 
 
     \t if(isset($_SESSION['size'])) { 
 
\t \t \t \t if($_SESSION['size'] == "full") { 
 
\t \t \t \t \t echo 'checked="checked" \t '; 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t ?> > 
 
     <label for="regularRadio">Full size</label> 
 
     <br>   \t 
 
     <input type="radio" name="size" value="thumb" id="thumbRadio" 
 
     <?php 
 
     \t if(isset($_SESSION['size'])) { 
 
\t \t \t \t if($_SESSION['size'] == "thumb") { 
 
\t \t \t \t \t echo 'checked="checked" \t '; 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t ?> > 
 
     <label for="thumbRadio">Thumbnail</label> 
 
     <br> 
 
     
 
     <input type="submit" name="submit"> 
 
    </form> 
 
</div> 
 

 
<?php 
 
$result = mysqli_query($con, "SELECT * FROM images WHERE img_size='thumb'"); 
 

 
while($row = mysqli_fetch_array($result)){ \t 
 
\t echo "<img src=".$row['img_name'] . " &nbsp; class='thumbnails' style='display:inline;float:left;'>"; 
 
\t \t 
 
} 
 
?> 
 

 
</body> 
 
</html>

+0

'renameImage() auf Ihrem renameImg hinzufügen' ist eine Menge zusätzliche Arbeit, wenn Sie einfach [pathinfo] (http://php.net/pathinfo) verwenden könnten. –

+0

danke für Ihre Antwort! Wie würde ich das anwenden? Es tut mir leid, ich bin immer noch ein Anfänger mit PHP ... – Melvin

+0

'$ f = pathinfo ($ _ FILES [blahblah]); $ NeuerName = $ f ['Basisname']. "_thumb". $ f ['extension']; ' –

Antwort

0

nur auf diese Weise Funktion

if(file_exists('_images/_galerie/' .$name)) 
    rename('_images/_galerie/' .$name, $thumb); 
0
You can check the file uploaded in uploads folder as follows: 
if(file_exists('path/'.$file_name){ 
if(move_uploaded_file($tmp_name,$location.$name)){ 
// file need to be upload in the uploads folder then upload the file into inner folder or may be same folder 
$thumb_image=$name.'_thumb'; 
if(copy(source,destination)){ 
echo 'file uploaded successfully'; 
// you can delete the file uploaded in the uploads folder as the file is uploaded in the inner folderes 
} 
} 
} 
Verwandte Themen