1

I use this Simple upload and resize image on PHP project to do my project. https://gist.github.com/zeuxisoo/910107/483c7b6082dcc484d7b4cee21aad12650c53e416 but I don't know how to rename the file after upload. I want to rename file and upload it into server as well as in database. The follow is code:

public function run() {
    $total = count($this->files);
    for($i=0; $i<$total; $i++) {
        if (empty($this->files['name'][$i]) === false) {
            if (move_uploaded_file($this->files['tmp_name'][$i], $this->store_directory.'/'.$this->files['name'][$i]) == false) {
                $this->errors['type'] = "run";
                $this->errors['file_name'] = $this->files['name'][$i];
            }
        }
    }

    return empty($this->errors);
}

I use a lot of method , eg basename It can't to it, any idea?? Thanks

francoleung
  • 237
  • 1
  • 7
  • 19
  • Possible duplicate of [How to rename uploaded file before saving it into a directory?](https://stackoverflow.com/questions/18705639/how-to-rename-uploaded-file-before-saving-it-into-a-directory) – Himanshu Upadhyay Dec 08 '17 at 04:59
  • Kindly accept any one answer... – GYaN Dec 08 '17 at 06:40

3 Answers3

1

Instead of using move_uploaded_file use it as per code example given below.

$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
1

You can do this

public function run() {
    $total = count($this->files);
    for($i=0; $i<$total; $i++) {
        if (empty($this->files['name'][$i]) === false) {

              $tempName = explode(".", $this->files['name'][$i]);
              $newName = "IMAGE_NAME"; //generate unique string to avoid conflict
              $newfilename = $newName . '.' . end($tempName );
            if (move_uploaded_file($this->files['tmp_name'][$i], $this->store_directory.'/'.$newfilename) == false) {
                $this->errors['type'] = "run";
                $this->errors['file_name'] = $this->files['name'][$i];
            }
        }
    }

    return empty($this->errors);
}

In $newName you can generate new name for your file.

Update

To avoid conflict use unique string each time when you generate the name.

Regolith
  • 2,944
  • 9
  • 33
  • 50
  • If we use a fixed string like `IMAGE_NAME` as you suggested, there would be a case that if a same file will be uploaded again, then the image with such a name on server will be replaced. So better to use some name convention which will help to avoid such situations. I have taken care of it in my answer. – Himanshu Upadhyay Dec 08 '17 at 06:01
1

Here is your solution

public function run(){
    $total = count($this->files);
    for($i=0; $i<$total; $i++){
        if(empty($this->files['name'][$i]) === false){
            $ext = substr(strtolower(strrchr($this->files['name'][$i], '.')), 1); //get the extension
            $new_name = 'new-name'.date('dmY').$ext;
            if (move_uploaded_file($this->files['tmp_name'][$i], $this->store_directory.'/'.$new_name) == false) {
                $this->errors['type'] = "run";
                $this->errors['file_name'] = $this->files['name'][$i];
            }
        }
    }
    return empty($this->errors);
}
GYaN
  • 2,327
  • 4
  • 19
  • 39