Page 1 of 1
Re-naming uploaded pictures
Posted: Tue Dec 13, 2011 11:18 am
by abbey4biz
Hello Programmers,
Please can somebody give me the codes that can be used rename an uploaded image in the database, using php and mysql, probably to rename the image to the id number of the field.
Below is the code I have used to upload the image to a folder on the server with the link of the image in the database.
Thanks.
if (isset($_FILES['upload'])) {
// Validate the type. Should be JPEG or PNG.
$allowed = array ('image/pjpeg', 'image/jpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG',
'image/PNG', 'image/png', 'image/x-png');
if (in_array($_FILES['upload'] ['type'], $allowed)) {
// Move the file over.
if (move_uploaded_file ($_FILES['upload']['tmp_name'], "uploads/{$_FILES['upload']['name'] }")) {
$temp = ("uploads/{$_FILES['upload']['name'] }");
if (empty($errors)) { // If everything’s OK.
Re: Re-naming uploaded pictures
Posted: Tue Dec 13, 2011 12:01 pm
by Amanda1998
Hope this put you on tracks... GL lo
Code: Select all
<?
// your code header
// your include files
// security process
//.......... so onnnn
}
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
$value1=urlencode('in case tha you wanna encode error result from external file.php');
/* path to your img folder */
$dir= "path/img_folder/";
/* declare a variable timename and give time() to be a new name of final file NAME */
$timename=time();
if (isset ($_POST['button']) && is_uploaded_file($_FILES['file']['tmp_name']))
{
$file= basename($_FILES['file']['name']);
/*_*/
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
/*_*/
if ($_FILES['file']['type'] != "image/gif" && $_FILES['file']['type'] != "image/jpeg" && $_FILES['file']['type'] != "image/png")
{
echo "<script>alert('file no allow it! file is not an image.')</script>";
}
elseif(exif_imagetype($_FILES['file']['tmp_name']) != IMAGETYPE_GIF && exif_imagetype($_FILES['file']['tmp_name']) != IMAGETYPE_JPEG)
{
echo "Bad file.";
echo "Click down to see our new report to Security staff blah blah ..";
echo "<a href=\"javascript:window.open('./error result from external file.php?cgi=$value1','name1','height=150,width=900');\"><font color=red>$connrep-$reip</font></a>";
/***** these are from my external security php files... gettin eery thing that we can from user conn ****/
$coid = $_POST['connid'];
$ip = $_POST['reip'];
$isp = $_POST['isp_or_proxy'];
$report = $_POST['connrep'].'-'.$_POST['reip'];
/* LETS BAN THE MF IP IF WICH BEEN TRYING TO UPLOAD THE CORRUPTED FILE */
$qry = "INSERT INTO my_blacklist_table(isp_or_proxy, ip, random, report, ..., ..., time) VALUES('$isp','$ip','$coid','$report', '...', '...', NOW())";
$result2 = @mysql_query($qry);
}
else
{
/* declare a NEW variable TO ADD TO THE timename WE GIVE IT FOR time() to be a new name of final file */
$ip = $_POST['ip'];
$file_name=$ip.'.'.time().'.'.$extension;
$newname=$dir.$file_name;
$file= $dir.$timename.basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $newname);
/*_*/
echo "<script>alert('File has been upladed ok!')</script>";
}
}
?>
Re: Re-naming uploaded pictures
Posted: Tue Dec 13, 2011 2:47 pm
by Amanda1998
You even can update other db with a other conn values from same call, said tha you have separate MySQL HOST's for Uploads and User's, then you make a new DB conn */
Code: Select all
<?php
/*....*/
/*...*/
/* declare a NEW variable TO ADD TO THE timename WE GIVE IT FOR time() to be a new name of final file */
$ip = $_POST['ip'];
$file_name=$ip.'.'.time().'.'.$extension;
$newname=$dir.$file_name;
$file= $dir.$timename.basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $newname);
/*** HERE ***/
$NewLinkToUserPic = include 'update_pic_table_link_on_db.php';
echo $NewLinkToUserPic;
/* do a query for select ip and username then post it */
$ip = $_POST['ip'];
$file_name=$ip.'.'.time().'.'.$extension;
$newname=$dir.$file_name;
$newlink = "http://yoursite.com/path/img_folder/$newname";
$usernameis = $_POST['username'];
$query = "UPDATE users SET foto = '$newlink' WHERE username = '$usernameis' ";
$result=mysql_query($query);
/*** TILL HERE ***/
echo "<script>alert('File has been upladed ok!')</script>";
}
}
?>
Where update_pic_table_link_on_db.php can be somthing like
Code: Select all
<?
/* This is update_pic_table_link_on_db.php */
$DB_HOST = "OtherHOST";
$DB_USERNAME = "OtherUN";
$DB_PASSWORD = "OtherPASS";
$DB_NAME = "OtherDB";
/* Connect to server and select database. */
mysql_connect("$DB_HOST", "$DB_USERNAME", "$DB_PASSWORD")or die("cannot connect to server");
mysql_select_db("$DB_NAME")or die("cannot select DB");
?>