Salve,
ho un sistema di CMS, che tra l'altro prevede l'upload delle immagini. Poichè queste andranno poi visualizzate nel sito, ovviamente devono avere necessariamente ciascuna un determinato nome.
Il problema è che, nel tempo, queste immagini potrebbero essere sostituite, per cui - ad es. - ad una immagine image01.jpg se ne dovrà sostituire un'altra, che si chiamerà sempre image01.jpg ma diversa.
Sfortunatamente, quando faccio l'upload delle immagini, se il sistema ne trova una uguale sul server carica la nuova, ma invece di sovrascrivere il file lo nomina facendolo precedere da un numero, ad es.: 6487image01.jpg.
Questo, è il codice che utilizzo per l'upload delle immagini:
Codice PHP:
<?php
class upload
{
var $file_permitted;
var $archive_dir;
var $max_filesize;
function upload ( $file_perm , $max_fil = 200000, $arc_dir = ".." )
{
if ( empty ($file_perm) ) $file_perm = array ("image/jpg","image/jpeg");
$this->file_permitted = $file_perm;
$this->max_filesize = $max_fil;
$this->archive_dir = $arc_dir;
}
function putFile ( $file )
{
$userfile_type = strtok ( $_FILES[$file]['type'], ";");
$userfile_name = $_FILES[$file]['name'];
$userfile_size = $_FILES[$file]['size'];
$userfile = $_FILES[$file]['tmp_name'];
$error = "This file type is not permitted: $userfile_type
";
$file_permitted = $this->file_permitted;
foreach ( $file_permitted as $permitted )
{
if ( $userfile_type == $permitted ) $error = "";
}
if ( ($userfile_size <= 0) || ($userfile_size > $this->max_filesize) ) $error = "File size error: $userfile_size Kb.
";
if ( $error == "" )
{
$filename = basename ($userfile_name);
if (!empty ($this->archive_dir) )
$destination = $this->archive_dir."/".$filename;
else
$destination = $filename;
if ( file_exists($destination) )
{
srand((double)microtime()*1000000);
$filename = rand(0,20000).$filename;
if (!empty ($this->archive_dir) )
$destination = $this->archive_dir."/".$filename;
else
$destination = $filename;
}
if(!is_uploaded_file($userfile)) die ("File $userfile_name is not uploaded correctly.");
if ( !@move_uploaded_file ($userfile,$destination) ) die ("Impossible to copy $userfile_name from $userfile to destination directory.");
return $destination;
}
else
{
// echo $error;
return false;
}
}
function chkgd2()
{
$testGD = get_extension_funcs("gd");
if (!$testGD)
{
echo "GD not even installed.";
return false;
}
else
{
ob_start();
phpinfo(8);
$grab = ob_get_contents();
ob_end_clean();
$version = strpos ($grab,"2.0 or higher");
if ( $version ) return "gd2";
else return "gd";
}
}
function miniatura ( $image_path, $path, $larghezza=100, $altezza=100, $pre_name="thumb_" )
{
if ( (eregi("\.png$", $image_path) || eregi("\.(jpg|jpeg)$", $image_path)) && $image_path )
{
$image_name = basename ( $image_path );
if (!empty ($path) )
$thumb_path = $path."/".$pre_name.$image_name;
else
$thumb_path = $pre_name.$image_name;
if ( $this->resize_jpeg($image_path, $thumb_path, $larghezza, $altezza) ) return $thumb_path;
else return "Error in thumbnail creation
";
}
elseif ($image_path) return "Impossible to create thumbnail by this kind of file
";
elseif ($send && $image_path) return "<font face=\"Verdana\" size=\"2\" color=\"red\">[b]Error in loading of image $cont[/b]</font>
";
}
function splitFilePath ( $completePath = "null/null" ) {
$filePath = array ( path=>"", filename=>"" ) ;
$tok = strtok($completePath,"/");
while ($tok) {
$file_name = $tok;
$tok = strtok("/");
}
$path = str_replace ( $file_name, "", $completePath );
$filePath[path] = $path;
$filePath[filename] = $file_name;
return $filePath;
}
}
?>
Come posso fare per risolvere? Tra l'altro, credo ci sia anche un problema di permessi (CHMOD), giusto?
Please, help...