How to upload and Save Files with Desired name using php

0 votes

I am using this code to upload files(images to a folder)

<form action='' method='POST' enctype='multipart/form-data'>
<input type='file' name='userFile'><br>
<input type='submit' name='upload_btn' value='upload'>
</form>

<?php
$target_Path = "images/";
$target_Path = $target_Path.basename( $_FILES['userFile']['name'] );
move_uploaded_file( $_FILES['userFile']['tmp_name'], $target_Path );
?>

when the file(image) is saved at the specified path... WHAT if i want to save the file with some desired name....

i have tried replacing THIS

$target_Path = $target_Path.basename( $_FILES['userFile']['name'] );

WITH THIS

$target_Path = $target_Path.basename( "myFile.png" );

BUT it's not working

Nov 4, 2020 in PHP by kartik
• 37,520 points
9,637 views

1 answer to this question.

0 votes

Hello,

You can try this,

$info = pathinfo($_FILES['userFile']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = "newname.".$ext; 

$target = 'images/'.$newname;
move_uploaded_file( $_FILES['userFile']['tmp_name'], $target);
answered Nov 4, 2020 by anonymous
• 82,880 points

Related Questions In PHP

0 votes
0 answers
0 votes
1 answer

How to delete all files from a folder using PHP?

Hello @kartik, Use this: $files = glob('path/to/temp/*'); // get ...READ MORE

answered Sep 14, 2020 in PHP by Niroj
• 82,880 points
1,361 views
0 votes
1 answer

How to upload and delete files from dropzone.js?

Hello, For deleting thumbnails you have to enable ...READ MORE

answered Nov 4, 2020 in PHP by Niroj
• 82,880 points
10,162 views
0 votes
1 answer

How to get the list of specific files in a directory using php?

Hello @kartik, You'll be wanting to use glob() Example: $files = ...READ MORE

answered Nov 6, 2020 in PHP by Niroj
• 82,880 points
2,206 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
22,958 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,969 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,900 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
44,398 views
0 votes
1 answer

How can I select and upload multiple files with HTML and PHP using HTTP POST?

Hello @kartik, This is possible in HTML5. Example (PHP ...READ MORE

answered Sep 15, 2020 in PHP by Niroj
• 82,880 points
1,975 views
0 votes
1 answer

How to get name of current directory using php?

Hello @kartik, To get only the name of ...READ MORE

answered Aug 27, 2020 in PHP by Niroj
• 82,880 points
4,668 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP