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.

5
  • 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) Commented Oct 23, 2014 at 14:58
  • Wow.. Sorry that was it, thanks haha! Commented Oct 23, 2014 at 15:02
  • Instead of using rand(), you could append current timestamp using time() function. You avoid collision. Commented Oct 23, 2014 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! Commented Oct 23, 2014 at 15:04
  • Something you are not doing... which would have caught that. ;) Commented Oct 23, 2014 at 15:04

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the stackoverflow panic guys, was missing a bracket :(
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.

4 Comments

You are also missing the bracket, same mistake as OP.
@Fred-ii- When you hear hoofbeats, you think horses, not zebras.
@Fred-ii- Joys of copy paste :). OP described the issue as files being overwritten, so did not pay detailed action to code.
@sjagr Where I live, it's more like "deer, moose and bears", but I'll take Zebras any day ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.