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
188 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

929 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

8 Experts available now in Live!

Get 1:1 Help Now