-3

I was wondering how to excactly change name while uploading a file. I have a text input "name", so I tried to put $_POST['name'] to moveuploadedfile function, but with no result. Where exactly should I place it?

html

<form id="upload" enctype="multipart/form-data" action="upload.php"method="post">

        <input type="text" name="name" placeholder="name" />
        <input type="text" name="watermark" placeholder="watermakr" />
        <input type="file" name="file" /><br />
        <input type="submit" value="send" />
</form>

php

$uploads_dir ='/usr/local/apache/htdocs/images/';
$target_file = $uploads_dir . basename($_FILES["file"]["name"]);

$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);


// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
echo "Only JPG, JPEG, PNG files are allowed.";
$uploadOk = 0;
}



if ($uploadOk == 0) {
echo "ERROR";
// if everything is ok, try to upload file
}
else {


if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {



    echo "File". basename( $_FILES["file"]["name"]). " was sent.";
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
pkk
  • 1
  • 5
  • You could either change the name of the file after you move it or you could change it as you move it. The choice is yours. – Jay Blanchard Jan 16 '15 at 13:02

2 Answers2

1

Old line:

move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)

New line:

move_uploaded_file($_FILES["file"]["tmp_name"], $uploads_dir . $_POST['name'])
Blaatpraat
  • 2,829
  • 11
  • 23
  • it doesn't work, It displays an error from my function checking if you uploaded only jpeg/png file. – pkk Jan 16 '15 at 13:07
  • Why change it? This won't work. OP wants to have a custom name for the "file". – Funk Forty Niner Jan 16 '15 at 13:09
  • And that custom name is in $_POST['name']. Now he saves the file as $_FILES['file']['name'], so the original name. – Blaatpraat Jan 16 '15 at 13:10
  • No, if the OP changes that to what you suggest, the file won't upload. Go over the question/code again. – Funk Forty Niner Jan 16 '15 at 13:13
  • So you use $target_file for checking? That piece of code is not included in your fragment, and that's my update does not work. In the future: please provide everything that is needed... See my updated code – Blaatpraat Jan 16 '15 at 13:14
  • @Fred-ii- This code changes the target name, not the original name. It should work if it wasn't that the OP does something weird to check the extension. – Blaatpraat Jan 16 '15 at 13:17
  • I already tried your updated code. It doesnt work with my check function, which I updated. – pkk Jan 16 '15 at 13:20
  • You've changed your answer from your original post http://stackoverflow.com/revisions/27984593/1 The OP now needs to try it. – Funk Forty Niner Jan 16 '15 at 13:21
-1

Instead of

if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
    echo "File". basename( $_FILES["file"]["name"]). " was sent.";

Use

    $ext = last(explode('.', $_FILES['file']['tmp_name']));
    $custom_name = 'something'; // or $_POST['name']
    $imageName = $custom_name.'.'.$ext;
    move_uploaded_file($_FILES["file"]["tmp_name"], $uploads_dir . $imageName);
Nepal12
  • 583
  • 1
  • 12
  • 29
  • it displays an error 'Parse error: syntax error, unexpected T_VARIABLE in /usr/local/apache/htdocs/upload.php on line 81' line 81 is '$newfilename = rand(1,99999) . '.' .end($temp); ' – pkk Jan 16 '15 at 13:18
  • updated the answer. See here `http://stackoverflow.com/questions/18705639/how-to-rename-uploaded-file-before-saving-it-into-a-directory` – Nepal12 Jan 16 '15 at 13:23