Solved

PHP rename file

Posted on 2007-04-06
4
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

636 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