Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialgiorgi mesturmanidze
Courses Plus Student 1,356 Pointsrename file name before uploading to server.
hello guys, here is my code. i want to change the file name of the images that users upload, and make them the same value as their name.
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
?>
1 Answer
Chris Shaw
26,676 PointsHi ,
PHP has a built in function called move-uploaded-file which allows you to target the temporary file uploaded on the server and move it to a new location along with a new name.
Example
<?php
$target_dir = "uploads/";
$target_file = $target_dir . date('d_m_Y_H_i_s') . '_'. $_FILES["fileToUpload"]["name"];
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
// Move the file
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);