-1

我有一个上传图片的上传表单,我想将图片的名称更改为特定的名称。

这是我的代码:

<?php

// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif','zip');

if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

$extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);

if(!in_array(strtolower($extension), $allowed)){
    echo '{"status":"error"}';
    exit;
}

if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'])){
    echo '{"status":"success"}';
    exit;
}
}

echo '{"status":"error"}';
exit;
4

5 回答 5

0

您可以在调用函数中添加自定义文件名move_uploaded_file。喜欢,

 if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.'yourcustomname'.$extension )){

如果要添加唯一名称,则可以使用like,

if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.time().$extension )){
于 2013-07-29T10:14:42.020 回答
0
if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/your_own_name_here.'$extension)){
    echo '{"status":"success"}';
    exit;
}
于 2013-07-29T09:14:48.023 回答
0

您应该在以下代码中更改$_FILES['upl']['name']

if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'])){

例如 :

if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/nameOfTheFile.jpg')){
于 2013-07-29T09:15:44.893 回答
0

更改以反映您的问题,将根据当前时间生成一个随机数,并附加原始上传文件的扩展名。

于 2015-11-23T11:25:13.367 回答
0
$temp = explode(".", $_FILES["file"]["name"]);

$newfilename = round(microtime(true)) . '.' . end($temp);

move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);
于 2015-11-23T12:22:22.637 回答