/    Sign up×
Community /Pin to ProfileBookmark

need PHP help to change file name

Hi, I have this script for uploading and deleting pictures on my site as a gallery.

When the public views my site, the pic are listed and given a name photo1 photo2 poto3 etc.

I want to be able to edit the name to call each picture whatever I want, please can you help 🙂

This is the script I use for uploading and deleting

[CODE]<?php

$user = ”;
$pass = ”;

session_start();

if(isset($_GET[‘log-out’]))
{
$_SESSION = array();
if (isset($_COOKIE[session_name()]))
{
setcookie(session_name(), ”, time()-86400, ‘/’);
}
session_destroy();
header(‘Location: http://’.$_SERVER[‘HTTP_HOST’].htmlentities($_SERVER[‘PHP_SELF’]));
die;
}
elseif(((!empty($_POST[‘user’]) and !empty($_POST[‘pass’]) and ereg($user.$pass, preg_replace(‘/s/’, ”, trim($_POST[‘user’]).trim($_POST[‘pass’])))) or (isset($_SESSION[‘logged’]) and ($_SESSION[‘logged’] === true) and isset($_SESSION[‘user’]) and ($_SESSION[‘user’] == $user))) and ($_SESSION[‘attempts’] < 5))
{
$_SESSION[‘logged’] = true;
$_SESSION[‘user’] = $user;
if(isset($_POST[‘user’]))
{
header(‘Location: http://’.$_SERVER[‘HTTP_HOST’].htmlentities($_SERVER[‘PHP_SELF’]));
}
}
else
{
top();
$_SESSION[‘attempts’] = isset($_SESSION[‘attempts’]) ? ++$_SESSION[‘attempts’] : 0;
print ‘<form action=”” method=”post”>
<p><label>Username: </label><input type=”text” name=”user”></p>
<p><label>Password: </label><input type=”password” name=”pass”></p>
<p><label>&nbsp</label><input type=”submit” value=”Enter”></p>
</form>’;
bottom();
die;
}

top();

echo ‘<p><a href=”‘.htmlentities($_SERVER[‘PHP_SELF’]).’?log-out”>Log-out</a></p>’;
if(count($_POST))echo ‘<p><a href=”‘.htmlentities($_SERVER[‘PHP_SELF’]).'”>Reset</a></p>’;

if(isset($_POST[‘add_delete’]) and (($_POST[‘add_delete’] == ‘add’) or ($_POST[‘add_delete’] == ‘delete’)))
{
$_SESSION[‘add_delete’] = $_POST[‘add_delete’];
if($_POST[‘add_delete’] == ‘add’)
{
add_form();
}
else
{
echo delete_form();
}
}
elseif(isset($_POST[‘step2’]))
{
if($_SESSION[‘add_delete’] == ‘add’)
{
echo add_image(‘./../images-fullsize/’, ‘./../images-thumbs/’);
add_delete_form();
}
elseif($_SESSION[‘add_delete’] == ‘delete’)
{
echo delete_fotos();
add_delete_form();
}
}
else
{
add_delete_form();
}

bottom();

function top()
{
?><!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN”
“http://www.w3.org/TR/html4/strict.dtd”>

<html lang=”es”>
<head>

<meta http-equiv=”content-type” content=”text/html; charset=iso-8859-1″>

<style type=”text/css”>
html, body{
margin:0;
padding:0.5em 2em;
font-size:1em;
}
form p label{
float:left;
width:8em;
padding-right:10px;
}

a{
color:#369;
text-decoration:none;
font-weight:bold;
}

a:hover{
color:#f63;
}

p{
clear:both;
}

p.text{
width:50%;
text-align:justify;
}

form.delete p{
float:left;
clear:none;
margin:0.5em;
}

span.warning{
color:#dd002b;
}

p label{
font-weight:bold;
}

.bold{
font-weight:bold;
}

.red{
font-weight:bold;
color:#f00;
}
</style>

<title>Gallery Admin</title>

</head>

<body>

<h2>Gallery Admin</h2>

<?php
}

function bottom()
{
?></body>

</html><?php
}

function add_delete_form()
{
?>
<form action=”” method=”post”>
<p><label>Add photos</label> <input type=”radio” name=”add_delete” value=”add” checked=”checked”></p>
<p><label>Delete photos</label> <input type=”radio” name=”add_delete” value=”delete”></p>
<p><label>Next step:</label> <input type=”submit” name=”submit” value=”OK”></p>
</form>

<?php
}

function delete_form()
{
$dir = “./../images-thumbs/”;

$content = null;

if(($count = count($glob = glob($dir.”*.jpg”))) > 0)
{
foreach($glob as $v)
{
$filemtime[] = filemtime($v);
}
array_multisort($filemtime, SORT_DESC, $glob);
$content .= ‘<p>Check the box next to each photo you want to delete and then press &quot;Delete&quot;.</p>’.”n”;
$content .= ‘<form action=”” method=”post” class=”delete”>’.”n”;
foreach($glob as $file)
{
$content .= ‘<p><img src=”‘.str_replace($_SERVER[‘DOCUMENT_ROOT’], ”, $file).'” alt=””><input type=”checkbox” name=”images[]” value=”‘.preg_replace(‘/.jpg$/’, ”, substr($file, strlen($dir))).'”></p>’.”n”;
}
$content .= ‘<p style=”clear:both”><input type=”submit” name=”step2″ value=”Delete”></p>’.”n”;
$content .= ‘</form>’.”n”;
}
return $content;
}

function delete_fotos()
{
if($count = count($_POST[‘images’]))
{
$thumbs = “./../images-thumbs/”;
$full = “./../images-fullsize/”;
foreach($_POST[‘images’] as $image)
{
if($image = preg_replace(‘/[^0-9]/’, ”, $image))
{
if(file_exists($thumbs.$image.’.jpg’))
{
unlink($thumbs.$image.’.jpg’);
}
if(file_exists($full.$image.’.jpg’))
{
unlink($full.$image.’.jpg’);
}
}
}
}
return ‘<p>’.$count.’ image’.(($count == 1)?”:’s’).’ deleted.</p>’.”n”;
}

function add_form()
{
?>
<p class=”text”>Use the form below to add images.</p>

<form action=”” method=”post” enctype=”multipart/form-data”>
<p><label>Photo</label> <input type=”file” name=”file”></p>
<p><label>Next step</label> <input type=”submit” name=”step2″ value=”OK”></p>
</form>

<?php

}

function add_image($full, $thumb)
{
$errors = array(1 => ‘php.ini max file size exceeded’,
2 => ‘html form max file size exceeded’,
3 => ‘file upload was only partial’,
4 => ‘no file was attached’);
if($_FILES[‘file’][‘error’] !== 0)
{
return ‘<p class=”red”>’.$errors[$_FILES[‘file’][‘error’]].”</p>n”;
}
if(!(@is_uploaded_file($_FILES[‘file’][‘tmp_name’])))
{
return “<p class=’red’>I can’t work on files that were not the subject of an upload.</p>n”;
}
if(!($details = @getimagesize($_FILES[‘file’][‘tmp_name’])))
{
return “<p class=’red’>I can only work on image files.</p>n”;
}
list($w, $h) = $details;
set_time_limit(60);
$filename = filemtime($_FILES[‘file’][‘tmp_name’]);
if(($w > 640) or ($h > 640) or (@$details[‘mime’] !== ‘image/jpg’))
{
ResizeConserveAspectRatio($_FILES[‘file’][‘tmp_name’], $full.$filename.’.jpg’, 640, 640);
}
else
{
copy($_FILES[‘file’][‘tmp_name’], $full.$filename.’.jpg’);
}
ResizeSemiAbstractTop($_FILES[‘file’][‘tmp_name’], $thumb.$filename.’.jpg’);
return ‘<p class=”red”>File: ‘.$_FILES[‘file’][‘name’].’ uploaded OK!</p>’.”n”;
}

function ResizeSemiAbstractTop($source, $destination, $w = 120, $h = 90)
{
$source_image = @file_get_contents($source)
or die(‘Could not open’.$source);
$source_image = @imagecreatefromstring($source_image)
or die($source.’ is not a valid image’);
$sw = imagesx($source_image);
$sh = imagesy($source_image);
$ar = $sw/$sh;
$tar = $w/$h;
if($ar >= $tar)
{
$x1 = round(($sw – ($sw * ($tar/$ar)))/2);
$x2 = round($sw * ($tar/$ar));
$y1 = 0;
$y2 = $sh;
}
else
{
$x1 = 0;
$y1 = 0;
$x2 = $sw;
$y2 = round($sw/$tar);
}
$slate = @imagecreatetruecolor($w, $h) or die(‘Invalid thumbnail dimmensions’);
imagecopyresampled($slate, $source_image, 0, 0, $x1, $y1, $w, $h, $x2, $y2);
@imagejpeg($slate, $destination, 100) or die(‘Directory permission problem’);
ImageDestroy($slate);
ImageDestroy($source_image);
return true;
}

function ResizeConserveAspectRatio($source, $destination, $w = 100, $h = 100)
{
$source = @imagecreatefromstring(
@file_get_contents($source))
or die(‘Not a valid image format.’);
$x = imagesx($source);
$y = imagesy($source);
if($w && ($x < $y)) $w = round(($h / $y) * $x); // maintain aspect ratio
else $h = round(($w / $x) * $y); // maintain aspect ratio
$slate = @imagecreatetruecolor($w, $h) or die(‘Invalid image dimmensions’);
imagecopyresampled($slate, $source, 0, 0, 0, 0, $w, $h, $x, $y);
@imagejpeg($slate, $destination, 85) or die(‘Directory permission problem’);
imagedestroy($slate);
imagedestroy($source);
return array($w,$h,’width=”‘.$w.'” height=”‘.$h.'”‘,’mime’=>’image/jpeg’);
}

?>[/CODE]

what do I need to add to enable me to edit the names?

thanks in advance

ps I am a complete newbie to php so I really appreciate your help 🙂

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@markandkelleauthorNov 01.2007 — HI, thanks for this, it does look good, but I am not sure how to add this to my script above, please can you help
Copy linkTweet thisAlerts:
@scragarNov 01.2007 — there are 2 methods I see for adding in an edit function, the first is to use the delete functions page to generate a link next to each image to allow for renaming, the second is to add an edit button to the delete function, validate against that and rewrite a lot of rows of information again to allow the user to change the file names.

either method will involve quite a few edits.


oh, and some of your functions don't return anything, yet you still echo the result. This will result in a performance hit if done too much, either remove the "echo" in front of your calls to said functions(mostly the add_form function), or edit how they return the information to improve efficiency.
×

Success!

Help @markandkelle 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...