Jump to content

Dynamic Upload path


crazy8

Recommended Posts

I have a small project im working on. I have a script that works as is right now but I have one more thing I would like to do. First off I will say this script is a combination of two other scripts I used in the past. This will be used in an administration area (for me only) so the security behind this isnt a big deal. So here we go. I have two input boxes as you can see, and I want to be able to type a directory (i.e /southdakota and /southdakota_thumbs) and if they dont exist, create them and if it does then upload images that I select and put them into that directory and the thumbs that get created to be put in the directory I have specified. I have looked up and down google and just can not find what I need to do this. So any help on how to do this would be greatly appreciated.

<style type="text/css">
fieldset {
background:#fff;
width: 655px;
}
#message {
font-size: .95 em;
color: #b80101;
font-style: italic;
font-weight: bold;
}
#delete {
clear: both;
width: 500px;
}
.menu {
float:left;
}
</style>

<?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 . '',150);
	//$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(150,150);
	// now to create thumbnails all the same size, create a 120x120 image, starting from the center
	//$thumb->cropFromCenter(200);
	$thumb->createReflection(0,0,0,true,'#ffffff');
	/// save the resized/cropped image to the thumbnails folder
	$thumb->save('imagethumbs/' . $newfilename . '',150);
	//$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" align="center">';
echo '<div class = "ntop"><h2>Upload Images</h2></div>';
echo '<div class = "nmiddle" align="center">';
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="center"><h2><font color="#b80101">Upload Images</font></h2></div>
    <form method="POST" enctype="multipart/form-data" name="image_upload_form" action="">
<fieldset>
    <div align="left">
    <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>
    
    <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>

Link to comment
https://forums.phpfreaks.com/topic/102547-dynamic-upload-path/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.