mcmne nk rename file upload???
7 replies [Last post]
does not have a status.
User offline. Last seen 3 years 22 weeks ago. Offline
Joined: 08/16/2009
Points: 0

mcmne nak rename file yg nak di upload???

mcmni jika 1st user upload file "file.php" then 2nd user pun upload file name yg sama "file.php" dalam folder yg sama..jd file 2nd user akan replace file 1st user td,mcmne nk bg file tadi x replace fail lama??

plzzz (Stare

does not have a status.
User offline. Last seen 4 years 48 weeks ago. Offline
Joined: 08/16/2009
Points: 0

Gunakan fungsi sha1_file()

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (isset($_FILES['pic']))
{
$file = $_FILES['pic'];

if ($file['error'] == UPLOAD_ERR_OK)
{
$name = sha1_file($file['tmp_name']);
$pathInfo = pathinfo($file['name']);
if (isset($pathInfo['extension']))
$name .= '.'.$pathInfo['extension'];

if (move_uploaded_file($file['tmp_name'], $uploaddir.$name))
{
// ..ok..
}
else
{
// ..failed..
}
}
}
}
?>

does not have a status.
User offline. Last seen 3 years 22 weeks ago. Offline
Joined: 08/16/2009
Points: 0

thanx for the script but still lg replace fail lama

2 user berbeza upload 1 file name yg sama tp kat folder upload cuma ader 1 fail jer. file baru dah replace fail lama..mcmne ek??

learn to fly
astora's picture
User offline. Last seen 3 years 36 weeks ago. Offline
Joined: 08/16/2009
Points: 3

mcm ni:

1. dapatkan nama file tuh
2. check nama file tu is_file()
3. Kalo false, upload saja mcm biasa
4. kalo true, kena wat byk kerja skit. aku kasi cth kat bwh:

<?php
$uploaddir = '/var/www/uploads/';

if(is_file($uploadfile)) {
$ext = getextension($_FILES['userfile']['name']);
$uploadfile = $uploaddir . 'nama_baru'.$ext;
} else {
$uploadfile = $uploaddir . $_FILES['userfile']['name'];
}
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);

function getextension($filename) {
$filename = strtolower($filename);
$extension = split("[/\\.]", $filename);
$n = count($extension)-1;
$extension = $extension[$n];
return $extension;
}

?>

learn to fly
astora's picture
User offline. Last seen 3 years 36 weeks ago. Offline
Joined: 08/16/2009
Points: 3

RALAT: *tak leh ke wat edit POST...tak mesra pengguna camni.

<?php
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . $_FILES['userfile']['name'];

if(is_file($uploadfile)) {
$ext = getextension($_FILES['userfile']['name']);
$uploadfile = $uploaddir . 'nama_baru'.$ext;
} else {
$uploadfile = $uploaddir . $_FILES['userfile']['name'];
}
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);

function getextension($filename) {
$filename = strtolower($filename);
$extension = split("[/\\.]", $filename);
$n = count($extension)-1;
$extension = $extension[$n];
return $extension;
}
?>

does not have a status.
User offline. Last seen 3 years 22 weeks ago. Offline
Joined: 08/16/2009
Points: 0

thanx astora Laughing out loud

masalah nie aku dah dpt wat

aku guna code :

<input type="file" name="FileToUpload">

<?php
if ($FileToUpload_type == 'image/gif') {
$type = '.gif';
}
if ($FileToUpload_type == 'image/pjpeg') {
$type = '.jpg';
}
if ($FileToUpload_type == 'image/x-png') {
$type = '.jpg';
}
$newfile = substr($FileToUpload, -9);
if($FileToUpload_name = '') {
print("No file was selected!");
}
elseif($FileToUpload_size > $MaxFileSize) {
print("The file to upload is too big");
}
else {
$global_db = mysql_connect('localhost', 'User', 'Password');
mysql_select_db('DBName', $global_db) or die("Connection error");
$query = "INSERT INTO Filetable (File) VALUES ('$newfile$type')";
$result = mysql_query($query) or die("ERROR");
move_uploaded_file($FileToUpload, "/full/path/on/your/server/images/$newfile$type");
}
?>

code nie akan tukar file name yg diupload.
k thanx to all

GIS,PHP
User offline. Last seen 2 years 16 weeks ago. Offline
Joined: 08/16/2009
Points: 0

Assalamualaikum n salam sejahtera..

sy punya coding lain skit..saje nak share.. Tongue

$picName=$_FILES['filePic']['name'];
$picType=$_FILES['filePic']['type'];
$picLoc=$_FILES['filePic']['tmp_name'];

//get newest id
$getId="select max(complaintId)+1 as complaintID from complaints_data_tbl";
$queryId=mysql_query($getId);
$resultId=mysql_fetch_array($queryId);
$newId=$resultId['complaintID'];

//rename file
$new=explode(".",$picName);
$newName=$newId.".".$new[1];
$path="../pictures/". $newName;
move_uploaded_file($picLoc, $path);

if( ($picType!='image/gif') && ($picType!='image/pjpeg') )
{
echo "Please choose file format GIF or JPEG";
}
else
{
$sql = "INSERT INTO complaints_data_tbl ";
$sql.= "(pic,picPath)";
$sql.= "VALUES ('$newName','$path')";
$query = mysql_query($sql);

if($query)
{
echo "The information have been successfully stored";
}
}

sy rename file kepada id.extension... cth:gambar.jpg..sy tukar kpd 253.jpg sbbnya nak nama file tu unik n sama dgn id file tu.. tp code sy ni ada prob kalau dalam nama file tu ada dot, cth:gambar.saya.jpg..