简体   繁体   English

PHP上传文件不重命名

[英]Php Upload File Without Rename

I have this code and i want to upload image with it's own name. 我有此代码,我想用自己的名字上传图片。

<?php
print_r($_FILES);
$new_image_name = "foto1.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"]
,"/home/izmircic/public_html/upload/".$new_image_name);?>  

I tried 我试过了

<?php
print_r($_FILES);
move_uploaded_file($_FILES["file"]["name"], "/home/izmircic/public_html/upload/");
?>  

but it didn't work. 但这没用。

The arguments to move_uploaded_file() are the current path including filename and the destination path including filename. move_uploaded_file()的参数是包含文件名的当前路径和包含文件名的目标路径。

It should be: 它应该是:

move_uploaded_file($_FILES["file"]["tmp_name"] ,"/home/izmircic/public_html/upload/".$_FILES["file"]["name"]);

You should also test for the return value, to see if it failed or not: 您还应该测试返回值,以查看返回值是否失败:

$moved = move_uploaded_file($_FILES["file"]["tmp_name"] ,"/home/izmircic/public_html/upload/".$_FILES["file"]["name"]);

if($moved){
    echo 'success';
} else {
    echo 'failed';
}

By allowing uploaded files to keep their original name, you need to validate it for whatever you're doing, for example if you only want images then check the file extension and load the image into the GD library to verify it's actually an image. 通过允许上传的文件保留其原始名称,您需要针对所执行的任何操作对其进行验证,例如,如果您仅想要图像,则检查文件扩展名并将该图像加载到GD库中以验证它实际上是图像。

For best security it would be better to store the files outside of the document root and use a PHP script to serve them as needed, and preferably with a generated filename instead of allowing the uploader to choose. 为了获得最佳安全性,最好将文件存储在文档根目录之外,并根据需要使用PHP脚本来提供文件,最好使用生成的文件名而不是允许上传者选择。

try this
<?php

print_r($_FILES);
$new_image_name = "foto1.jpg";  // remove this one and replace $_FILES["file"]["name"]
move_uploaded_file($_FILES["file"]["tmp_name"]
        , "/home/izmircic/public_html/upload/" . $_FILES["file"]["name"]);
?>

I think this might work for you 我认为这可能对您有用

move_uploaded_file($_FILES["file"]["tmp_name"],".//home/izmircic/public_html/upload/".$_FILES["file"]["name"]); move_uploaded_file($ _ FILES [“ file”] [“ tmp_name”],“ .// home / izmircic / public_html / upload /".$_ FILES [” file“] [” name“]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM