Solved

PHP rename file

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL limit and not so limited 13 61
Session time out quickly. 6 54
Delete image(s) associated with record(s) 16 46
Ajax success not firing alert 6 42
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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.
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.
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 …

734 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