/    Sign up×
Community /Pin to ProfileBookmark

scandir, for loops and renaming permissions. Help.

I’m trying to write a script that’ll rename every file in a directory with a number and regular file extension.

Here’s what I’ve got

[code=php]<?php

$dir = ‘/wampseverwwwexperimentsfiles\’;
# ^^ Give the absolute file path to the folder where your files are, between those quotes.
#forward slash means root, and put 2 backslashes at the end.

$ext = ‘.gif’;
# ^^ Give the extension that you wish the files to have

$allfiles = scandir($dir);
foreach ($allfiles as $number => $name){
if($number != (‘1’ || ‘2’)){ #That bit is so that it doesn’t bother with the root or parent wich was confusing things.
$orginalfile = $dir.$name;
$newfile = $dir.$number.$ext;
/*This is line 15 */ rename($orignalfile,$newfile);
}
}

?>[/code]

And this is the error I’ve got

[quote]

Warning: rename(,/wampseverwwwexperimentsfiles.gif) [function.rename]: Permission denied in f:wampseverwwwexperimentsmassrename.php on line 15

[/quote]

As you can tell I’m running this on a WAMP server.

The odd thing is the way that the rename function is reported in the error misses out the original file path, why is this? And more importantly, how can I fix it?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@artoAug 14.2005 — Line 13: $or[B]gi[/B]nalfile

Line 15: $or[B]ig[/B]nalfile

Could be the problem ?

Arto
Copy linkTweet thisAlerts:
@the_treeauthorAug 14.2005 — Woah thanks Arto. *slaps forhead*
Copy linkTweet thisAlerts:
@bokehAug 14.2005 — I haven't tested the following so it could need a debug:

[code=php]<?php

$dir = '/wampseverwwwexperimentsfiles\'; // what is this with backslashes. Try changing them to forward.
$ext = '.gif';

$handle = opendir($dir);
$i = 1;
while (false !== ($file = readdir($handle))){
if(is_file($file){ // checks it is a file not a directory
if(getimagesize($file){ // checks it is an image
if(copy($file, $dir.$i.$ext)){
unset($file); // deletes the old file
$i++;
}
}
}
}

closedir($handle);

?>[/code]
×

Success!

Help @the_tree spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...