Solved

I have a large collection of files I need to rename - but the PHP functions "copy" and "rename" are disabled on my server.  Is there an alternative I can try?

Posted on 2008-10-19
4
187 Views
Last Modified: 2008-10-19
I have a large collection of files I need to rename based on an ID number in my database.  This would be extremly simple to do with a "rename" or "copy" function, but the server I am hosted with seems to have these two functions disabled.  Is there another way of doing this through PHP?  I don't know much about batch (DOS maybe?) files, but maybe there is a way that PHP could output some sort of batch script so I could rename my local files instead?  I have XP Home, so unfortunatly I can't setup a local host and just run the PHP script on my own machine.

Liz
0
Comment
Question by:maeve100
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
Scripting_Guy earned 500 total points
ID: 22752910
why can't you? Just download PHP for Windows (http://www.php.net/get/php-5.2.6-Win32.zip/from/a/mirror) and extract it to a folder, like c:\php.

Then open a cmd window and use as follows:
c:\php\php.exe -f c:\script\script.php

PHP scripts on Windows do not need a line for the interperter if called with php.exe, so delete the #!/usr/bin/php line if it is there. Just start your scripts with <?php
0
 

Author Comment

by:maeve100
ID: 22753053
The local renaming of the file worked - but I can't seem to connect to my online database to grab the name I need to rename the file with based on the ID, Scripting_Guy.  Should I be able to run a local file and have it connect to a database online?  Maybe its a simple matter of syntax?  I am positive the dbconc.php file is working correctly.
<?php 
 

require_once ('dbconc.php'); // Connect to the db.
 

// Make the query.

$query = "SELECT * FROM list WHERE list.id = 9";

$result = @mysql_query ($query); // Run the query.

$row = mysql_fetch_array ($result, MYSQL_ASSOC);
 

$file = 'tmp_file.txt';

$newfile = $row['OID'];

rename($file, $newfile);
 

mysql_close(); // Close the database connection.

?>

Open in new window

0
 
LVL 3

Expert Comment

by:Scripting_Guy
ID: 22753237
This depends on your database server, but if you're on a shared host i doubt it.

You can try by change your mysql_connect statement from "localhost" as server (presumably) to the external ip of your server.

But as mentioned, i doubt that this will work. So I would export the data from your online database (e.g. into a textfile) and work with that textfile on your local machine.

If you'll give me a bit more details about your current db scheme (what is the old file name - what is the new one) I'll help you with that if you need any help.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22753478
Consider the following(note the permissions on "somedir" in the example allows for the writing and deletion of files):
<?php

$oldname='somedir/oldfile.txt';

$newname='somedir/newfile.txt';

$fp = fopen($newname, 'w');

fwrite($fp,file_get_contents($oldname));

fclose($fp);

unlink($oldname);

echo 'done';

?>

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
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.

757 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

22 Experts available now in Live!

Get 1:1 Help Now