Solved

PHP rename file

Posted on 2007-04-06
4
292 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
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.

839 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