PHP Classes

Integrate your script

Recommend this page to a friend!

      Easy upload resize thumb image  >  All threads  >  Integrate your script  >  (Un) Subscribe thread alerts  
Subject:Integrate your script
Summary:Inclusion
Messages:7
Author:Graham Peach
Date:2009-09-07 15:27:49
Update:2009-09-08 16:39:27
 

  1. Integrate your script   Reply   Report abuse  
Picture of Graham Peach Graham Peach - 2009-09-07 15:27:49
Hello Mr Ubaidullah Khan

My nam is Graham and I am hoping you can help me out

I already have a script which I use to upload upto 3 different images.

It saves the image to a directory called pictures on the server and holds the path to that image in a mysql database.

Along with the image it also inserts some text data into 2 or 3 other fields.

It is run like a blog, where the user uploads 1 image and the text for the days blog, the script automatically inserts todays date so that we can display the blog in order.

At the moment the image is uploaded in whatever size the photograph was taken and I just use a width parameter in the IMG tag to make the image thumbnail size, which is fine, but when you click the image, it is usually huge, something like 3000 pixels wide, far too much.

I have been trying without success to follow the php guides but I am hopelessly lost, hen I saw your script which I think I could use to dowhat I want. I have been trying all sorts of include methods etc but I just dont know enough php to make it happen.

Is you script able to do what I want and if so how much amending to the original is required.

I am a relative novice when it comes to php, but I am trying to learn. I would appreciate your help

Kind Regards

Graham Peach

ps. I can show you my script if it is any use, assuming you will be kind enough to help me.



<?php

$uploadDir = 'pictures/';
$fileUploaded = 0; // set a flag for use later

// connection parameters !

include('/home/sites/xxxxxxxxxx.com/db_config/blog_config.php');

$database = mysql_connect($hostname,$username,$password) or die("Could not connect to server");
mysql_select_db($table,$database) or die("Could not find table");

//$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
//mysql_select_db($dbname);
////////////////////////////////////

if(isset($_POST['upload']))
{

$id = $row['id'];
$title = addslashes($_POST['title']);
$detail = addslashes($_POST['detail']);
//$precis = addslashes($_POST['precis']);
//$year = addslashes($_POST['year']);

// check if a file has been uploaded
if($_FILES['userfile']['error'] != 4) { // error code 4 meaning that no image is present
$fileUploaded = 1;
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

// the files will be saved in filePath
$filePath = $uploadDir . $fileName;

// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
$result = move_uploaded_file($tmpName, $filePath);

if (!$result) {
echo "Error uploading file";
exit;
}
} else {
// if no file added, generate empty variables so as not to break the query
$fileName = '';
$fileSize = '';
$fileType = '';
$filePath = '';
}

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);

}

$query = "INSERT INTO xxxxxxxx (name, size, type, path, title, detail) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$title', '$detail')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

mysql_close($database);

// if a file was uploaded, the fileUploaded variable will be set to 1
if($fileUploaded == 1) {
echo "<br>File uploaded<br>";
// show the image after upload - comment this out if not required - good for checking though :)
echo '<img src="'. $uploadDir .'/'.$_FILES['userfile']['name'][$i] .'" >';
$movedtolocation="pictures/".$_FILES['upload']['name'];# basically, take the location of wher the image is stored..
}

}
?>
<link href="styleadmin.css" rel="stylesheet" type="text/css">


<form action="" method="post" enctype="multipart/form-data" name="uploadform">
<table width="609" align="center" cellpadding="1" cellspacing="1">
<tr>
<td colspan="4" align="center">&nbsp;</td>
</tr>
<tr>
<td width="165"><p class="subtitle">Main Title :</p></td>
<td width="437" colspan="3"><p> <input name="title" type="text" size="35">&nbsp;</td>
</tr>
<tr>
<td width="165" valign="top">&nbsp;</td>
<td colspan="3">&nbsp;</td>
</tr>
<tr>
<td width="165" valign="top"><p class="subtitle"> Blog :</p>
<p align="center" class="small">&nbsp;</p></td>
<td colspan="3"><textarea name="detail" id="detail" cols="50" rows="20"></textarea></td>
</tr>
<tr>
<td width="165"><p class="subtitle">Select Picture:</p></td>
<td colspan="3"><p align="center">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" class="box" id="userfile" size="35">
<span class="small">&nbsp;&nbsp;&nbsp;&nbsp;<br>
Leave blank if no picture to upload</span><br>
<br>
</p></td>
<tr>
<td width="165">&nbsp;</td>
<td colspan="3">&nbsp;</td>
</tr>
<tr><td colspan="4" align="center"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>

  2. Re: Integrate your script   Reply   Report abuse  
Picture of Graham Peach Graham Peach - 2009-09-07 15:29:32 - In reply to message 1 from Graham Peach
Just noticed that the script i included is the one for a single image which is the one i use

Thanks

  3. Re: Integrate your script   Reply   Report abuse  
Picture of ubaidullah khan ubaidullah khan - 2009-09-08 01:06:33 - In reply to message 1 from Graham Peach
thanks for using my class and contacting me.

I have integrate my class in your code take a look on code bellow. i have also include two file in your code, just bellow you include your config file. please give them right path ie. where these two files are located, because I don't where you save them on your server.

Also I will like to know the address of website where you are using my class,

if you have still any difficulties or need some more help, feel free to ask me. I will try my best to solve your problem

thanks

------------------------ you page code --------------------------

<?php

$uploadDir = 'pictures/';
$fileUploaded = 0; // set a flag for use later

// connection parameters !

include('/home/sites/xxxxxxxxxx.com/db_config/blog_config.php');

//must include these files and give right path to files where these are located on your server
include('imageresizer.class.php');
include('upload_class.php');

$database = mysql_connect($hostname,$username,$password) or die("Could not connect to server");
mysql_select_db($table,$database) or die("Could not find table");

//$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
//mysql_select_db($dbname);
////////////////////////////////////

if(isset($_POST['upload']))
{

$id = $row['id'];
$title = addslashes($_POST['title']);
$detail = addslashes($_POST['detail']);
//$precis = addslashes($_POST['precis']);
//$year = addslashes($_POST['year']);

// check if a file has been uploaded
if($_FILES['userfile']['error'] != 4) { // error code 4 meaning that no image is present
$fileUploaded = 1;
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

///========================= changes by Obaid
$UF_obj = new Upload();
$UF_obj -> File = $_FILES['userfile'];
$UF_obj -> SavePath = $uploadDir; // PLACE where you want to save images.

//I have comment below line. so thumnail of picture will not create. as I understand your code. you don't want to create
//thumb of image.
//if you want themb then uncomment this line and give path where you want to store thumb of picture

//$UF_obj -> ThumbPath = $uploadDir.'thumb/'; //if not specify will not create thumbnil

$UF_obj -> NewName = $_FILES['userfile']['name'];

//width and height of large image which will save in "pictures/" folder
$UF_obj -> NewWidth = 400;
$UF_obj -> NewHeight = 400;
////width and height of thumb image which will save in "pictures/thumb/" folder
$UF_obj -> TWidth = 100;
$UF_obj -> THeight = 100;

/*
* if you want to name image something other then upload image name then use bellow formate
* for example you upload two images then
*
* $UF_obj -> NewName = array('NewName1.jpg', 'NewName2.jpg');
*/

// $UF_obj -> NameCase = 'lower'; //default no change. upper for upper case

$UF_obj -> OverWrite = true; //default = true. replace existing image

//UploadFile() function upload and resize image.
//function return error message if any.
//error variable is in array form. so you can get more then one error/warning messages.
// or you can also access error message by class object varialbe like $UF_obj -> Error

$Error = $UF_obj -> UploadFile(); //this function upload and resize file
//print_r($Error);
if(count($Error) > 0 and is_array($Error))
{
foreach($Error as $key=>$val)
{
$fileUploaded = 0;
echo $val . '<br>';
}
}
///========================= end changes by Obaid


// the files will be saved in filePath
$filePath = $uploadDir . $fileName;

//******************************************************* old code remove this ***********************************************
// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
//$result = move_uploaded_file($tmpName, $filePath); I have comment this line

/*if (!$result) {
echo "Error uploading file";
exit;
}*/
//******************************************************* old code remove this ***********************************************

} else {
// if no file added, generate empty variables so as not to break the query
$fileName = '';
$fileSize = '';
$fileType = '';
$filePath = '';
}

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);

}

$query = "INSERT INTO xxxxxxxx (name, size, type, path, title, detail) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$title', '$detail')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

mysql_close($database);

// if a file was uploaded, the fileUploaded variable will be set to 1
if($fileUploaded == 1) {
echo "<br>File uploaded<br>";
// show the image after upload - comment this out if not required - good for checking though :)
echo '<img src="'. $uploadDir .'/'.$_FILES['userfile']['name'].'" >';
$movedtolocation="pictures/".$_FILES['upload']['name'];# basically, take the location of wher the image is stored..
}

}
?>
<link href="styleadmin.css" rel="stylesheet" type="text/css">


<form action="" method="post" enctype="multipart/form-data" name="uploadform">
<table width="609" align="center" cellpadding="1" cellspacing="1">
<tr>
<td colspan="4" align="center">&nbsp;</td>
</tr>
<tr>
<td width="165"><p class="subtitle">Main Title :</p></td>
<td width="437" colspan="3"><p> <input name="title" type="text" size="35">&nbsp;</td>
</tr>
<tr>
<td width="165" valign="top">&nbsp;</td>
<td colspan="3">&nbsp;</td>
</tr>
<tr>
<td width="165" valign="top"><p class="subtitle"> Blog :</p>
<p align="center" class="small">&nbsp;</p></td>
<td colspan="3"><textarea name="detail" id="detail" cols="50" rows="20"></textarea></td>
</tr>
<tr>
<td width="165"><p class="subtitle">Select Picture:</p></td>
<td colspan="3"><p align="center">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" class="box" id="userfile" size="35">
<span class="small">&nbsp;&nbsp;&nbsp;&nbsp;<br>
Leave blank if no picture to upload</span><br>
<br>
</p></td>
<tr>
<td width="165">&nbsp;</td>
<td colspan="3">&nbsp;</td>
</tr>
<tr><td colspan="4" align="center"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>

  4. Re: Integrate your script   Reply   Report abuse  
Picture of ubaidullah khan ubaidullah khan - 2009-09-08 01:28:57 - In reply to message 1 from Graham Peach
oh sorry I forget to tell you that I have do little changes in upload_class.php, you may also get mail of change from phpclasses.org. but changes is important for you, that is related to uploading single file.

please before testing your code download new file from phpclasses.org


regards
Obaidullah khan

  5. Re: Integrate your script   Reply   Report abuse  
Picture of Graham Peach Graham Peach - 2009-09-08 10:26:00 - In reply to message 4 from ubaidullah khan
Hi there,

Thanks for not only your swift reply but also for changing my code for me. I am very grateful, not many people would do that.

I have downloaded the new file and uploaded everything to my server having made the relevant changes regarding the database and paths etc.

when i try to open the main script file on the web i get this error

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/sites/grap.co.uk/public_html/blog/upload_class.php on line 16

The file i am trying to load on the web is the one you amended for me and is found at http://www.grap.co.uk/blog/blogadmin.php

Do you know what is wrong

Many thanks again -


  6. Re: Integrate your script   Reply   Report abuse  
Picture of ubaidullah khan ubaidullah khan - 2009-09-08 16:39:27 - In reply to message 5 from Graham Peach
I have check code of upload_class.php and find nothing/no error

I am unable to get any idea :(

did you make changes to files i have sent to you. if yes then inform me.....

if your problem didn't solve then you can get me on messanger on

gtalk
ubaid23 [at] gmail [dot] com
and msn

ubaid23 [at] hotmail [dot] com


regards

  7. Re: Integrate your script   Reply   Report abuse  
Picture of lee charles lee charles - 2015-08-18 07:46:38 - In reply to message 3 from ubaidullah khan
Hi.
Thanks for your nice sharing. But I wonder how can I use these codes. Do I need another image resizer (http://www.pqscan.com/convert-pdf/to-image-size-csharp.html) to help change the size of images? I am almost a green hand here. Any suggestion will be appreciated. Thanks in advance.



Best regards,
Arron

 
For more information send a message to info at phpclasses dot org.