Jump to content

Dynamic Upload path


crazy8

Recommended Posts

I have an upload form I am working on. Right now it all works great but rather than it uploading to a hard coded path I would like the form to be able to upload an image and a created thumb to a user defined path (i.e /image, /image/thumb), via two input boxes. Here is what I have right now for code. I know the code is a little rough its still a WIP.

 

<?php 
include_once ('db_connect_images.php'); 
?>

<?php
//This page allows users to upload files tot he server
//Set the page title and include the header
$page_title = 'Upload an Image';

//include ('db_scripts/db_connect_insert_2.php');
//$db->connect('images');


if (isset($_POST['action']))
{
$imageDir = 'image/'; //// root directory to main images folder with trailing slash
$imageName = $imageDir . $_FILES['image_file']['name']; //// 

if (move_uploaded_file($_FILES['image_file']['tmp_name'], $imageName))
{	

	$array = explode(".", $_FILES['image_file']['name']);
	$nr    = count($array);
	$ext  = '.' . $array[$nr-1];	

	///// Insert data into the DB
	$insert = "INSERT INTO images 
	(dir, ext, comment, date_added) 
	VALUES 
	('" . $imageDir . "', '" . $ext . "', '$_POST[comment]', NOW())";
	$insertresults = mysql_query($insert) or die(mysql_error());
	$lastpicid = mysql_insert_id();

	$newfilename =  $lastpicid . $ext;
	rename($imageName, $imageDir . $newfilename);
	include_once('class.thumbnail.php');

	////// TAKE THE ORIGINAL IMAGE AND RESIZE IT TO A SLIGHTLY SMALLER ONE 

	//// go and get the uploaded image
	$large = new Thumbnail('image/' . $newfilename . '');
	//// and resize it to dimensions equal to or smaller than 800x600
	$large->resize(800,600);
	//// now save it 
	$large->save('image/' . $newfilename . '',100);
	//$large->destruct(); //only needed for PHP4

	////// NOW TAKE THE NEWLY RESIZED IMAGE AND CREATE A THUMBNAIL FROM IT

	// go and get the resized image
	$thumb = new Thumbnail('image/' . $newfilename . '');
	// resize it to no larger than 200x200
	$thumb->resize(100,100);
	// now to create thumbnails all the same size, create a 120x120 image, starting from the center
	//$thumb->cropFromCenter(200);
	$thumb->createReflection(50,50,50,true,'#ffffff');
	/// save the resized/cropped image to the thumbnails folder
	$thumb->save('imagethumbs/' . $newfilename . '',100);
	//$thumb->destruct(); //only needed for PHP4

	if ($insertresults)
	{
		$message = 'Image Added';
	} 
	else 
	{
		$message = 'Image Failed';
	}
}
	else
{
	$message = 'Image Failed to Upload';
}	
}
if (isset($_POST['delete']))
{
foreach ($_POST as $id => $value)
{
	if (is_numeric($id) && $value == 'delete')
	{
		$sql = mysql_query("SELECT * FROM images WHERE id = '". $id ."'");
		$rows = mysql_fetch_array($sql);

		$query = "DELETE from images WHERE id = '" . $id . "'";
		$result = mysql_query($query);

		$largeimg = unlink('image/' . $id . $rows['ext'] . '');
		$thumbimg = unlink('imagethumbs/' . $id . $rows['ext'] . '');	
	}		
}
}

?>

<?php
echo '<div class = "nround">';
echo '<div class = "ntop"><h2>Upload Images</h2></div>';
echo '<div class = "nmiddle" align="left">';
echo '<fieldset class="display" align="center">';
echo '<div class="display" align="center" >';
$sql = "SELECT * FROM images ORDER BY id ASC";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
if ($count == 0)
{
echo 'You have not uploaded any pictures yet!';
}
else
{
echo '   <form action="" method="POST">';
while ($rows = mysql_fetch_array($query))
{
	echo'<div style="border: 2px solid #fff; margin: 5px; float: left;">
<div>
<a href="images/' . $rows['id'] . $rows['ext'] . '" rel="lightbox"><img src="imagethumbs/' . $rows['id'] . $rows['ext'] . '"/></a>  
</div>
<div>
<input type="hidden" name="image_name" value="' . $rows['image_name'] . '">
<input type="checkbox" name="' . $rows['id'] . '" value="delete">
</div>
</div>';
}
echo '<div id="delete"><input type="submit" name="delete" value="Delete Images">    </div>
</form>';
}
echo '</div>';
echo '</fieldset>';
?>

<div align="left"><h2><font color="#b80101">Upload Images</font></h2></div>
    <form method="POST" enctype="multipart/form-data" name="image_upload_form" action="">
<fieldset>
    <p>Image Folders Location <input type="text" size="32" name="folder" value="" /></p>
<p>Thumbnail Folder Location <input type="text" size="32" name="thumbfolder" value="" /></p>
    
    <div align="left">
    <p>Upload Image: <input type="file" name="image_file" size="20"><br>
    <font size="1">Click browse to upload a local file</font><br>
    Comment: <textarea name="comment" rows="2" cols="25"></textarea>
    <br>
    <input type="hidden" name="type" value="multiple" />
    <input name="action" type="submit" value="Upload Image">
    <input type="hidden" name="type" value="multiple">
</div>
</fieldset>
</form>
<?php
// we first include the upload class, as we will need it here to deal with the uploaded file
include_once'class.upload.php';

if ($_POST['type'] == 'multiple') {

$folder=$_POST['folder'];
$thumbs=$_POST['thumbfolder'];

            $handle->Process($folder);
            // we check if everything went OK
            if ($handle->processed) {
                // everything was fine !
                echo '<fieldset>';
                echo '  <legend><h3>Large Image Uploaded</h3></legend>';
                echo '  <p>' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB</p>';
                echo '  View Image Uploaded: <a href="'.$folder.'' . $handle->file_dst_name . '">' . $handle->file_dst_name . '</a>';
                echo '</fieldset>';
            } else {
                // one error occured
                echo '<fieldset>';
                echo '  <legend><h3>Large Image Not Uploaded</h3></legend>';
                echo '  Error: ' . $handle->error . '';
                echo '</fieldset>';
            }
            $handle->Process($thumbs);

            // we check if everything went OK
            if ($handle->processed) {
                // everything was fine !
                echo '<fieldset>';
                echo '  <legend><h3>Thumbnail Created and Uploaded</h3></legend>';
                echo '  <p>' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB</p>';
                echo '  View Thumbnail Uploaded: <a href="'.$thumbs.'' . $handle->file_dst_name . '">' . $handle->file_dst_name . '</a>';
                echo '</fieldset>';
            } else {
                // one error occured
                echo '<fieldset>';
                echo '  <legend><h3>Thumbnail not created and not uploaded</h3></legend>';
                echo '  Error: ' . $handle->error . '';
                echo '</fieldset>';
            }
            
        } else {
      		
}
?>

I have looked up and down google for a few days now and I just cant seem to find anything even close to what Im looking to do even though I know people have done this over and over. Any help on this would be very greatly appreciated.

 

Thanks much

Link to comment
Share on other sites

You already have the fields to put in the paths although I would make them a drop down box. It would not be a good idea to just let someone choose a path to send files to. Then when you submit the form you can grab the paths. Looking at your form you would do something like

 

 

$image_location = $_POST['folder'];
$thumb_location = $_POST['thumbfolder'];
$imageDir = 'image/'; //// root directory to main images folder with trailing slash
$imageName = $imageDir . $image_location . $_FILES['image_file']['name']; //// 
        $thumbName = $imageDir. $thumb_location . $_FILES['image_file']['name'];

 

then for the thumb

$large->save($thumbName,100);

 

Ray

Link to comment
Share on other sites

  Well this is going to be used in an administration environment so security isnt really an issue, but the reason I wanted it set up so you could just type in a path is that the path and image info will also be submitted to a db. So all of it will actually take care of a number of steps so I dont have to :D

As I am sure you may (or may not) have noticed is that this code is kind of a compilation of a few projects. I suppose I could make the root path the directory that the script sits in then use the form to create the image paths as needed inside it. In case it helps, here is a little scenario.

  Im putting together a very small site for trips I take and photos i take on the trips. So lets say my upload script/form sits in a folder called "root" and I go to South Dakota. I take a ton of pictures and then want to upload them to the server. So in the input box of the form, for the path of the image im uploading, I type "/southdakota" (full path would then be /root/southdakota) then I type the path for the thumb that gets created as "/southdakota/thumbs". Now if these directories dont exist then id like the script to create them otherwise just add the images to the paths.

 

I dont know if that changes the approach at all but thats what I am looking to do.

 

thanks for your input

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.