Reputation: 12538
I am working on an application that allows users to upload a picture to the system and we store this picture on cloud storage (S3). Currently, once the picture is uploaded we're processing their file by copying the temp file locally via move_uploaded_file()
and then using that local file to upload it to S3. Is this local copy a necessity or is it possible to override this step and directly upload to cloud storage without ever storing a local copy?
$temp = $_FILES['file']['tmp_name'];
$target = ABSPATH.'uploads/';
$name = $_FILES["file"]["name"];
$targetFile = $target . $name;
move_uploaded_file($temp,$targetFile);
//upload local file to s3
$this->saveToCloud($this->s3Path,$targetFile);
Before the file gets copy to local storage via move_uploaded_file
it lives in tmp php storage:
/Applications/MAMP/tmp/php/phpx3uBiT
What I'd like to do is simply:
$this->saveToCloud($this->s3Path,$_FILES["file"]["tmp_name"]);
Thanks in advance!
Upvotes: 1
Views: 385
Reputation: 544
If you are thinking about performance issues then you should NOT care about move_uploaded_file
because it just do some checks and just rename it's name(path). CPU consume is minimal. Unless the destination of the temporary file is in an external hard disk and it will need network transportation.
If you want to make server process lighter then you can consider using AWS SDK Javascript for browser and do the file upload directly from client browser to your S3 bucket (check this link for how to do it: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html#s3-example-photo-album-adding-photos). Using this way you should take in consideration making a HTTP Request just to save file information on your database after/before file uploads.
If you are not interested using AWS SDK Javascript for browser then you can try:
$this->saveToCloud($this->s3Path,$_FILES['file']['tmp_name']);
but I'm not sure if it works, I didn't try it.
I hope this helps
Upvotes: 1