heya!
i've got a basic image upload script on my server that i learned and edited based off of this tutorial. mine looks like this:
<?php
// Create the main upload function
function do_upload() {
// Create an array containing all valid upload file types for this script
$allowed_types = array(
"image/gif" => "gif",
"image/pjpeg" => "jpg",
// Add more types here if you like
);
// Check to see if the file type is in the allowed types array
if(!array_key_exists($_FILES['userfile']['type'], $allowed_types)) {
die("Invalid file type.");
}
// Where will the file be uploaded to?
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . "/blog/2005/02/";
// What is the files temporary name?
$file = $_FILES['userfile']['tmp_name'];
// What is the files actual name?
$filename = $_FILES['userfile']['name'];
// Does this file already exist on the server?
if(file_exists($uploaddir . $filename)) {
die("A file with that name already exists on this server.");
} else {
// This file does not already exist, so copy it.
copy($file, $uploaddir.$filename) or die("Could not copy file.");
}
// All done! :-)
echo "Upload successful";
}
?>
<html>
<head>
<title>Image Upload Script by Musicalmidget</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="do_upload">
<input type="file" name="userfile"><br />
<input type="submit" name="submit" value="Upload Image">
</form>
</body>
</html>
<?php
// If the form has been completed, execute the upload function (above).
if($_POST['action'] == "do_upload") {
do_upload();
}
?>
it works great, but now, i wanna get a bit tricky! 🙂
right now, the image uploads to a path i configure in the php code, which is:
/blog/2005/02/
however, i'd like the ability to sometimes upload to "blog/2005/02" and sometimes upload to "blog/2005/03"
is there anyway to determine where i'm uploading the image to? i.e; a "browse" function on my server that acts similar to the browse function on my hard drive -- where it'd connect to "blog/2005" and allow me to decide which folder to enter.
possible?