1

How to upload default image file in folder PHP? I am trying to upload selected file or default file in particular folder, but I can't make good.

target_file = $target_dir . time()."_abc.jpg";

or

basename($_FILES["files"]["name"]="abc.jpg";
3

2 Answers 2

0

You cannot upload directly by using the filename. Every file has a temporary path where it is stored when it is uploaded. this is referred to by tmp_name. You'll have to upload this tmp_name to the specific directory. Consider below code:

$target_file = $target_dir . time()."_abc.jpg";
if (file_exists($target_file)) {
   if (move_uploaded_file($_FILES['files']['tmp_name'], $target_file)) {
       echo "File Uploaded";
   } else {
       echo "File Not Uploaded";
   }
}

Hope this helps.

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

Comments

0

Its not clear about the scenario in which you are trying to upload default file or selected file. Generally if one selected a file then only the need to upload the file arises otherwise you just keep a default file in folder and show that. If you are concerned about file uploads in php, you need to follow certain procedures to upload files into folder using any programming language. For the case of PHP first need to check in php.ini whether

file_uploads = On if it is On then you can proceed for file uploading. The form should enable enctype to enctype="multipart/form-data"

Next you can restrict the image format that is to be uploaded, image size etc based on different php functions belong to $_FILES variable.

Please refer this

https://www.w3schools.com/Php/php_file_upload.asp

Comments

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.