在上载到服务器之前重命名文件 [英] Renaming a file before uploading to server

查看:167
本文介绍了在上载到服务器之前重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在手机上创建某种自拍上传器。现在它工作的很好,只是当我在iPhone上测试上传文件时,所有的照片都会被命名为image.jpeg,所以它们会互相覆盖。现在我想要做的是重命名文件,让我们说image1.jpeg和下一个image2.jpeg上传到服务器之前。



我目前的代码:

 <?php 
if(isset($ _ FILES ['myFile'])){
move_uploaded_file($ _ FILES ['myFile'] ['tmp_name'],uploads /。$ _FILES ['myFile'] ['name' ]);
回声'成功';
}
?>

我试过这个代码,给我的图像一个图像的文件名值加一个1到99999之间的随机数但是这不是成功的。

 <?php 
if(isset($ _ FILES ['myFile' ])){

$ temp = explode(。,$ _ FILES [myFile] [name]);
$ newfilename = rand(1,99999)。 '。'.end($ temp);
move_uploaded_file($ _ FILES [myFile] [tmp_name],uploads /。$ newfilename;
echo'successful';
}
?>

任何指针都会被理解。

解决方案


这不是一个成功的方法。


不是描述性的描述你的错误,请包括PHP错误或自己调查,以便你(或我们)能够弄清楚你的代码有什么问题,有些编辑甚至会告诉你你的解析错误在哪里,你也可以使用PHP linter 。

错误在于这一行:

$ $ $ $ $ $ $ $ $ move_uploaded_file $ _FILES [myFile] [tmp_name],uploads /。$ newfilename;

move_uploaded_file 函数缺少一个右括号,所以您必须将其放回:

  move_uploaded_file($ _ FILES [myFile] [tmp_name],uploads /。$ newfilename); 

我也推荐一个更好的随机文件名生成器。尝试这样的代替︰

  $ newfilename = sha1(uniqid(mt_rand(),true))。 '。'.end($ temp); 

它会创建一个哈希表碰撞的可能性要低得多(阅读:1中的2 160 。)



祝你好运

I'm trying to create some sort of "selfie" uploader on mobile phones. Now it's working great it's just that when i test the upload file on my iPhone all the photo's will be named image.jpeg so they keep overwriting each other. Now what i want to do is rename the file to let's say image1.jpeg and the next image2.jpeg before it gets uploaded to the server.

My current code:

<?php
if (isset($_FILES['myFile'])) {
move_uploaded_file($_FILES['myFile']['tmp_name'], "uploads/" . $_FILES['myFile']    ['name']);
echo 'successful';
}
?>

I tried this code to give my image a filename value of image plus a random number between 1 and 99999 but this wasn't a succes.

<?php
if (isset($_FILES['myFile'])) {

$temp = explode(".",$_FILES["myFile"]["name"]);
$newfilename = rand(1,99999) . '.' .end($temp);
move_uploaded_file($_FILES["myFile"]["tmp_name"], "uploads/" . $newfilename;
echo 'successful';
}
?>

Any pointers will be appreciated.

解决方案

this wasn't a succes.

Not a descriptive way to describe your error. Please include PHP errors or investigate them yourself so you (or we) can figure out what problems you're having with your code. Some editors will even tell you where your parsing errors are. You can also use a PHP linter.

The error lies in this line:

move_uploaded_file($_FILES["myFile"]["tmp_name"], "uploads/" . $newfilename;

The move_uploaded_file function is missing a closing bracket, so you must put it back in:

move_uploaded_file($_FILES["myFile"]["tmp_name"], "uploads/" . $newfilename);

I'd also recommend a better random filename generator. Try something like this instead:

$newfilename = sha1(uniqid(mt_rand(), true)) . '.' .end($temp);

It will create a hash that has a much lower likelihood of collisions (read: 1 in 2160.)

Best of luck!

这篇关于在上载到服务器之前重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆