0

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.

user2099810
  • 361
  • 5
  • 15
  • 3
    And what errors do you get? `move_uploaded_file($_FILES["myFile"]["tmp_name"], "uploads/" . $newfilename;` is incorrect, use `move_uploaded_file($_FILES["myFile"]["tmp_name"], "uploads/" . $newfilename);` (you were missing a closing bracket)... finally you should come up with something more randomized as you can easily have collisions with your existing code. Try using `sha1(uniqid(mt_rand(), true))` instead of `rand(1,99999)` – sjagr Oct 23 '14 at 14:58
  • Wow.. Sorry that was it, thanks haha! – user2099810 Oct 23 '14 at 15:02
  • Instead of using `rand()`, you could append current timestamp using `time()` function. You avoid collision. – marian0 Oct 23 '14 at 15:03
  • No problem, I've added my comment as an answer since it solved your problem. Please feel free to accept it if it helped you! – sjagr Oct 23 '14 at 15:04
  • [**Something you are not doing...**](http://php.net/manual/en/function.error-reporting.php) which would have caught that. ;) – Funk Forty Niner Oct 23 '14 at 15:04

2 Answers2

2

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!

Community
  • 1
  • 1
sjagr
  • 15,983
  • 5
  • 40
  • 67
0

I think your approach is not quite correct.

Renaming the file at client end assumes that each client will have a unique file naming convention. However, in practice this would be impossible, if not very difficult to implement.

You can however, much more easily change the file naming convention on the server.

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

$userid = 1; // User id loaded from database or some other way to identify user
$temp = explode(".",$_FILES["myFile"]["name"]);

// Create a distinct name for the file
$newfilename = $userid . '.' . date_timestamp_get() '.' .end($temp);

move_uploaded_file($_FILES["myFile"]["tmp_name"], "uploads/" . $newfilename);
echo 'successful';
}
?>

The above code, rather than using a random number generator, ensure a unique key based on the user's id, and the current timestamp, along with the filename. Other variations such as GUID could also be used.

Kami
  • 19,134
  • 4
  • 51
  • 63