Friday, 23 August 2013

renaming a file PHP mySQL

renaming a file PHP mySQL

I have a problem with renaming pictures in my gallery, here I have a
picture named
FIRST TIME PublicImg-0123456789.jpg and that's the ID to define the img
SECOND PublicImg-0123456789[_I_ADD_TITLE_HERE_FIRST].jpg till now it works
PERFECTLY but when I decide to rename it again
It works by this way
PublicImg-0123456789[_I_ADD_TITLE_HERE_FIRST][_I_ADD_TITLE_HERE_SECOND].jpg



I can't find a way to keep ONLY the ID and add a text, and I don't wanna
keep in my TABLE the ORIGIN urls because I have urls for the image and the
thumbnail and a small image too, if I add three columns this will be so
ugly.
so guys any ideas ? :D


BTW here is the Code
/////////
// Fichier IMAGE a renommer
$nom_image_old = preg_replace
('#./Images/'.$_SESSION['username'].'/#','', $_GET['img'] );
$nom_image_old = preg_replace ('#.jpg#','', $nom_image_old );
// Fichier Miniature a renommer
$nom_thumb_old = preg_replace
('#./Images/'.$_SESSION['username'].'#',
'./Thumbnails/'.$_SESSION['username'].'' ,$_GET['img']);
// Fichier Resized a renommer
$nom_resized_old = preg_replace
('#./Images/'.$_SESSION['username'].'/#',
'./Images/'.$_SESSION['username'].'/resized/' ,$_GET['img']);
$req2 = $BDD->prepare('SELECT * FROM images_users WHERE
img_uploader = :username AND img_url = :url ');
$req2->execute(array('username'=> $_SESSION['username'], 'url' =>
$_GET['img'] ));
while ($donnees = $req2->fetch())
{
if ( isset($_POST['rename'])
AND isset($_GET['img'])
AND isset($_POST['renommer'])
AND !empty($_POST['renommer'])
AND (strlen($_POST['renommer'])) <= 30)
{
$nom_image_new =
str_replace('.'.$infosfichier['extension'],
'_'.strip_tags($_POST['renommer']).'.'.$infosfichier['extension']
,$donnees['img_url']);
$nom_thumb_new =
str_replace('.'.$infosfichier['extension'],
'_'.strip_tags($_POST['renommer']).'.'.$infosfichier['extension']
,$donnees['thumb_url']);
$nom_resized_new =
str_replace('.'.$infosfichier['extension'],
'_'.strip_tags($_POST['renommer']).'.'.$infosfichier['extension']
,$donnees['resized_url']);
// on remplace les escpaces par des underscore (_) grace
la fonction preg_replace
rename ( $_GET['img'] = preg_replace('#[ |:]#',
'_',$_GET['img']) , $nom_image_new = preg_replace('#[
|:]#', '_', $nom_image_new) ) ;
rename ( $nom_thumb_old = preg_replace('#[ |:]#',
'_',$nom_thumb_old ) , $nom_thumb_new = preg_replace('#[
|:]#', '_',$nom_thumb_new) ) ;
rename ( $nom_resized_old = preg_replace('#[ |:]#',
'_',$nom_resized_old) , $nom_resized_new =
preg_replace('#[ |:]#', '_',$nom_resized_new) ) ;
header('Refresh: 0; url=affichage.php?img='.
$nom_image_new .'');
$req2->closeCursor();
// on rennome les données existant dans la table
$req = $BDD->prepare('UPDATE images_users SET
img_url
=:NEW_img_url,
thumb_url
=:NEW_thumb_url,
resized_url
=:NEW_resized_url,
img_description
=:NEW_img_description,
img_modification = NOW()
WHERE img_uploader =:username
AND img_url
=:img_url_old
');
$req->execute(array(
'NEW_img_url' =>
$nom_image_new,
'NEW_thumb_url' =>
$nom_thumb_new,
'NEW_resized_url' =>
$nom_resized_new,
'NEW_img_description' => '',
'username' =>
$_SESSION['username'],
'img_url_old' => $_GET['img'],
));
exit();
}}
/////////

No comments:

Post a Comment