Solved

PHP rename file

Posted on 2007-04-06
4
285 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:kiranvj
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:kiranvj
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now