【问题标题】:PHP change file name on upload for thumbnailsPHP在上传缩略图时更改文件名
【发布时间】:2016-07-06 12:59:12
【问题描述】:

如果选择了单选按钮“缩略图”,我将在文件名中添加“_thumb”,如果选择了“全尺寸”,则不要添加它(保持文件名不变)。这适用于我的数据库条目,但不适用于实际文件。我怀疑我以某种方式将“_thumb”添加到两种类型的文件 - 缩略图和全尺寸 - 因为在我上传到的文件夹中,我只看到添加了“_thumb”的那个。我不知道是什么问题,请帮忙!

到目前为止,这是我的 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;

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

}
?>

<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" 
			<?php 
			if (isset($_SESSION['category'])) {
				if($_SESSION['category'] == "black"){ 
					echo ' selected'; }}
			?> >black</option>
            <option value="colour" 
			<?php
            if (isset($_SESSION['category'])) {
				if($_SESSION['category'] == "colour"){ 
					echo ' selected'; }}
			?> >colour</option>
        </select>
        
        	<br>           
        	
        <input type="radio" name="size" value="full" id="regularRadio"        
        <?php
        	if(isset($_SESSION['size'])) {
				if($_SESSION['size'] == "full") {
					echo 'checked="checked"	';
				}
			}
		?> >
        <label for="regularRadio">Full size</label>
        <br>        	
        <input type="radio" name="size" value="thumb" id="thumbRadio"
        <?php
        	if(isset($_SESSION['size'])) {
				if($_SESSION['size'] == "thumb") {
					echo 'checked="checked"	';
				}
			}
		?> >
        <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)){	
	echo "<img src=".$row['img_name'] . " &nbsp; class='thumbnails' style='display:inline;float:left;'>";
		
}
?>

</body>
</html>

【问题讨论】:

  • renameImage() 是很多额外的工作,而您只能使用 pathinfo
  • 感谢您的回复!我将如何应用它?对不起,我还是 PHP 的初学者...
  • $f = pathinfo($_FILES[blahblah]); $newname = $f['basename'] . '_thumb' . $f['extension'];
  • 在我将 'basename' 更改为 'filename' 之后,这行得通...如果您将其作为答案,我可以将其标记为正确的...谢谢!

标签: php file-upload file-rename


【解决方案1】:

只需在 renameImg 函数中添加这种方式

if(file_exists('_images/_galerie/' .$name))
    rename('_images/_galerie/' .$name, $thumb);

【讨论】:

    【解决方案2】:
    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
    }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-06
      • 2013-03-12
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      • 2018-07-01
      • 2016-12-17
      • 2018-01-21
      相关资源
      最近更新 更多