PHP Forums Archive

Merging the Upload script

Tags: upload

Squidge posted on 2010-05-28 18:18:17 #

Hello All,

Great site :)

I am learning PHP (trying to) and would like to use the PHP upload script.

However I have a small PHP snippet that allows me to select a Directory to upload to as this will change depending on what is being uploaded.

<div style="float:left;"><form action="index_test.php" method="post">
<input type="submit" value="Home">
</form></div>
<?php
If ($_POST["lookhere1"])
$serchme1 = &$_POST["lookhere1"];
else
$serchme1 = "PhotoAlbum";

?>
<form action="index_test.php" method="post">
<input type="submit" value="change">
<select name="lookhere1">
<?php
$images = glob("$serchme1/*");
foreach($images as $image)
{
$amdir1 = is_dir($image);
If ($amdir1){
echo("<option value='$image'>$image</option>");
}
}
?>
</select>
</form>

What I would like to ask is how can I merge the 2 together so I can get the use of the upload script, and be able to select which directory to upload to??

From what I understand the location in the drop down box is stored in $serchme1

Your help would be greatly appreciated

Comments / discussions

Olaf posted on 2010-05-28 19:50:03 #

Hello and welcome,

is this question about the upload script hosted on finalwebsites?

If you like to switch the upload directory with a select menu. Give this menu numeric values and match them against an array of "real" values. This is needed to make your script safe.

Squidge posted on 2010-06-02 18:18:57 #

Olaf thanks for getting back. Yes this is refering to your Upload Script.

Is the array created with in the upload script?

I am very new to PHP so please explain like you would to a child :)

Olaf posted on 2010-06-02 19:05:02 #

There are several types of variables in PHP, check this PHP manual page:
http://www.php.net/manual/en/language.variables.external.php

Squidge posted on 2010-06-02 19:29:47 #

ok had a look over it, unfortunatly not in depth.

My array:

<?
$category = array(
1=> "Image Folder 1",
2=> "Image Folder 2",
3=> "Image Folder 3",
4=> "Image Folder 4",
5=> "Image Folder 5",
);
$category = str_replace(" ", " ", $category);

echo '<SELECT name=category>';
foreach ($category as $key => $value)
{
echo '<OPTION value='.$value.'> '.$value.'';
}
echo '</select>';

?>

Which gives me my drop down menu.

How can I use this to point as the folder to upload to?

$foto_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/files/"; // "files" is the folder for the uploaded files (you have to create these folder)
$foto_upload->foto_folder = $_SERVER['DOCUMENT_ROOT']."/files/photo/";
$foto_upload->thumb_folder = $_SERVER['DOCUMENT_ROOT']."/files/thumb/";
$foto_upload->extensions = array(".jpg"); // specify the allowed extension(s) here

Your help is greatly appreciated

Squidge posted on 2010-06-02 19:31:32 #

I have tried this:

$foto_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/files/"; // "files" is the folder for the uploaded files (you have to create these folder)
$foto_upload->foto_folder = $_SERVER['DOCUMENT_ROOT']."/files/" . $category;
$foto_upload->thumb_folder = $_SERVER['DOCUMENT_ROOT']."/files/thumb/";

But this only uploads to the /files/ location :(

Olaf posted on 2010-06-02 19:33:53 #

try $_REQUEST['category'], what you're trying isn't possible anymore these days

Squidge posted on 2010-06-02 19:40:25 #

Olaf, thanks so much for your time.

I have this now :

$foto_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/files/"; // "files" is the folder for the uploaded files (you have to create these folder)
$foto_upload->foto_folder = $_SERVER['DOCUMENT_ROOT']."/files/".$_REQUEST['category'];
$foto_upload->thumb_folder = $_SERVER['DOCUMENT_ROOT']."/files/thumb/";

But this is giving me an error:

Undefined index: category in B:\xampp\XXX.php on line 28

This line is :

$foto_upload->foto_folder = $_SERVER['DOCUMENT_ROOT']."/files/".$_REQUEST['category'];

Olaf posted on 2010-06-02 19:45:27 #

this happens if you don't posted the form (yet), right?

Check the code for the example file to understand how "posting a form" works

Squidge posted on 2010-06-02 19:58:17 #

hmmm... I am lost :'(

<?php

$category = array(
1=> "Microcyb",
2=> "Google",
3=> "YABB",
4=> "Macromedia",
5=> "PHP",
);
$category = str_replace(" ", " ", $category);

include ($_SERVER['DOCUMENT_ROOT']."/classes/upload/foto_upload_script.php"); //classes is the map where the class file is stored (one above the root)

$max_size = 1024*250; // the max. size for uploading

define("MAX_SIZE", $max_size);
$foto_upload = new Foto_upload;

$foto_upload->upload_dir = $_SERVER['DOCUMENT_ROOT'].'/files/'.$_REQUEST["category"]; // "files" is the folder for the uploaded files (you have to create these folder)
$foto_upload->extensions = array(".jpg"); // specify the allowed extension(s) here
$foto_upload->language = "en";
$foto_upload->x_max_size = 900;
$foto_upload->y_max_size = 900;
$foto_upload->x_max_thumb_size = 90;
$foto_upload->y_max_thumb_size = 90;

if (isset($_POST['Submit']) && $_POST['Submit'] == "Upload") {
	$numfiles = 4; // enter the number of file fields here
	for ($i = 0; $i < $numfiles; $i++) {
		if (!empty($_FILES['upload']['tmp_name'][$i])) {
			$foto_upload->the_temp_file = $_FILES['upload']['tmp_name'][$i];
			$foto_upload->the_file = $_FILES['upload']['name'][$i];
			$foto_upload->http_error = $_FILES['upload']['error'][$i];
			$foto_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "n"; // because only a checked checkboxes is true
			$foto_upload->do_filename_check = "n";
			if ($foto_upload->upload()) {
				$foto_upload->process_image(false, true, true, 80);
				$foto_upload->message[] = 'Processed foto: '.$foto_upload->file_copy.'!
				file_copy.'">

';
			}
		}
	}
}
$error = $foto_upload->show_error_string();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Photo-upload form</title>

<style type="text/css">
<!--
body {
	text-align:center;
}
label {
	margin:0;
	float:left;
	display:block;
	width:120px;
}
#main {
	width:350px;
	margin:0 auto;
	padding:20px 0;
	text-align:left;
}
-->
</style>
</head>
<body>
<div id="main">
  <h1>Photo-upload form</h1>
<?php
echo '<SELECT name=category>';
foreach ($category as $key => $value)
{
echo '<OPTION value='.$value.'> '.$value.'';
}
echo '</select>';
?>
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
	<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_size; ?>">
	<div>
	  <label for="upload">Select a foto</label>
		<input type="file" name="upload[]" size="35">
		<input type="file" name="upload[]" size="35">
		<input type="file" name="upload[]" size="35">
		<input type="file" name="upload[]" size="35">
</div>
    <div>
      <label for="replace">Replace an old foto?</label>
    <input type="checkbox" name="replace" value="y"></div>
	<p style="margin-top:25px;text-align:center;"><input type="submit" name="Submit" id="Submit" value="Upload">
	</p>
  </form>
  <p><?php echo $error; ?></p>
</div>
</body>
</html>

What is it I am doing wrong here?

Olaf posted on 2010-06-02 20:04:25 #

replace this row

$foto_upload->upload_dir = $_SERVER['DOCUMENT_ROOT'].'/files/'.$_REQUEST["category"];

with

$cat = '';
if (!empty($_REQUEST["category"])) $cat = $_REQUEST["category"];
$foto_upload->upload_dir = $_SERVER['DOCUMENT_ROOT'].'/files/'.$cat;

Squidge posted on 2010-06-02 20:14:01 #

Thats gotten rid of the PHP error. But is not uploading to the folder :(

Squidge posted on 2010-06-02 20:34:18 #

Does it make any difference that I am using XAMMP as a dev area?

Olaf posted on 2010-06-02 20:34:20 #

Quote from: Squidge
"Thats gotten rid of the PHP error. But is not uploading to the folder :("

start with simple folder names and try debugging like described here:
http://www.finalwebsites.com/forums/topic/simple-php-code-debugging

Actually you're doing things upside down, first learn the PHP basics and than start with stuff like file uploads. At the moment you're writing a script which invites hacker to break your server.

It's great to see people starting stuff, but in your case you might have bugs/errors in every code you write. Buy some PHP beginner book and read / practice first