Solved

PHP rename file

Posted on 2007-04-06
4
293 Views
Last Modified: 2008-01-09
Hi,

I was wondering how I get php to change the name of a file in a folder which is below (a child folder to) the location of the php file. I have set up an upload of a file, however I want it to change the name everytime it is uploaded from "filename.ext" to e.g. "hello.txt", and so it will write over any existing files with that name. I have put my php config file below as it might help.

<?
$target_path = "files/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']).
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

$file_old = "files/"['uploadedfile'];
$file_new = "files/hello.txt";

if(!rename($file_old, $file_new))
{
    echo ("Rename failed");
}
?>

Thanks

Mark
0
Comment
Question by:mark_06
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Kiran Paul VJ
ID: 18864832
i think there is some problem with this line

$file_old = "files/"['uploadedfile'];

try this

$file_old = "files/".basename( $_FILES['uploadedfile']['name']);


kiranvj
0
 
LVL 16

Expert Comment

by:Kiran Paul VJ
ID: 18864853
some mistake in the above post

try this

$file_old = $target_path;

and try echo $file_old to see if its correct path

kiranvj
0
 
LVL 15

Assisted Solution

by:Tomeeboy
Tomeeboy earned 50 total points
ID: 18864897
I believe this is something close to what you're wanting to do, and it doesn't even require using rename().  Take a look (modified example from PHP.net):

<?php
  if(!empty($_FILES["uploadedfile"]))
  {
    $uploaddir = $_SERVER['DOCUMENT_ROOT']."/files/";
    $uploaddir.="hello.txt";
   
    //Copy the file to some permanent location
    if(move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $uploaddir))
    {
      echo "File Uploaded!";
    }
    else
    {
      echo "There was a problem when uploding the new file!";
      print_r($_FILES);
    }
  }
?>

This should overwrite hello.txt every time a new file is uploaded.  You may need to change the folder path that is added on to the document root, since all I knew about your preferred destination was the folder name "files".

Hope that helps!
0
 
LVL 14

Accepted Solution

by:
raja_ind82 earned 200 total points
ID: 18864953
Upload.php
**********************************
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>


Uploader.php
*********************************************
$target_path_root = "files/";
$tmpName  = $_FILES['uploadedfile']['tmp_name'];
$fileSize = $_FILES['uploadedfile']['size'];
$fileType = $_FILES['uploadedfile']['type'];
 
$target_path = $target_path_root . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']).
    " has been uploaded"."<br>";
} else{
    echo "There was an error uploading the file, please try again!";
}

$file_old = $target_path;
$file_new = $target_path_root."/hello.txt";

if(file_exists($file_new))
      unlink($file_new);

if(!rename($file_old, $file_new))
{
    echo ("Rename failed");
}
else
{
      echo "Renamed Successfully.";
}
?>
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question