2016-07-05 8 views
0

「サムネイル」ラジオボタンが選択されている場合、ファイル名に「_thumb」を追加します。「フルサイズ」の場合はファイル名をそのまま残します"が選択されます。これは私のデータベースのエントリでは動作しますが、実際のファイルでは動作しません。サムネイルとフルサイズの両方のファイルに「_thumb」を追加すると思うのですが、アップロードするフォルダには「_thumb」が追加されたファイルのみが表示されるためです。私は何が間違っているのか理解できません、助けてください!サムネイルのアップロード時にPHPがファイル名を変更する

これは、これまでのところ、私のPHPです:

<?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は()'がたくさんあります[pathinfo](http://php.net/pathinfo)を使用しているときには余分な作業が必要になります。 –

+0

お返事ありがとうございます!どのように私はそれを適用するだろうか?申し訳ありませんが、私はまだPHPの初心者です... – Melvin

+0

'$ f = pathinfo($ _ FILES [blahblah]); $ newname = $ f ['basename']。 '_thumb' $ f ['extension']; ' –

答えて

0

ちょうどあなたrenameImg関数にこの方法を追加

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 
} 
} 
} 
関連する問題