上传后需要重命名图像

时间:2012-08-22 07:31:21

标签: php

我在这里有这个代码输出一个图像..我需要改变它,因为目前它给了我类似的东西:test.jpg,我需要的是它给我test_s.jpg

我想使用重命名功能!

$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetPath = str_replace('//','/',$targetPath);
$targetFile = $targetPath . $_FILES['Filedata']['name'];
tamano_nuevo_foto($stempFile, 420, $stargetFile);

4 个答案:

答案 0 :(得分:2)

你可以这样做:

$extension = array_pop( explode(".", $_FILES['Filedata']['name']) ); //get extension
$targetFile = $targetPath . "some_new_name".$extension;
tamano_nuevo_foto($tempFile, 420, $targetFile);

答案 1 :(得分:0)

首先:您似乎有一条可由用户操纵的路径。您在路径中直接使用$ _REQUEST ['folder']是不好的。用户可以将ANYTHING放在那里,甚至像../../../之类的东西来移动你的文件系统!

要更改名称,只需:

$ targetFile = $ targetPath。 “myfilename.png”;

答案 2 :(得分:0)

或者您可以使用此功能:

function add_s($file){
    $fName = substr($file, 0,strpos($file, "."));
    $fExtension = substr($file, strpos($file, "."));
    $newFName = $fName."_s".$fExtension;
    return $newFName;
}

答案 3 :(得分:0)

您应该使用pathinfomove_uploaded_file

$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/'; // should make this more secure, like a fixed path or in a whitelist
$targetPath = str_replace('//','/',$targetPath);
$ext = pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION);
$basename = pathinfo($_FILES['Filedata']['name'], PATHINFO_BASENAME);
$targetFile = $targetPath . $basename . "_s" . $ext;
move_uploaded_file ( $tempFile , string $targetFile)
//tamano_nuevo_foto($stempFile, 420, $stargetFile); // move and resize ??