Jump to content

Recommended Posts

I've been building an image uploader recently, but one that allows separate users with separate directories. So, everything's working fine, I have delete functions working, and all completely flat-filed. There's no problem as such, but the one last thing I'd like is a filing system that uses folders. I know how I would allow users to make their folders more organized. I have no idea how to go about this, so I thought I'd ask you guys. Here is all the code you should need.

 

The uploader code:

 

<?
//print_r($_POST);

if($_POST["action"] == "Upload Image")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";


$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";

if(empty($error))
{
$newimage = $f_user . "/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}

}

?>

<center><form method="POST" enctype="multipart/form-data" name="image_upload_form" action="<?$_SERVER["PHP_SELF"];?>">
<input type="file" name="image_file" size="20">
<br><br><input type="submit" value="Upload Image" name="action">
</form></center>
<div align="center"><br>
  <?
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
?>

<?php
$files = array();
$dir=opendir($f_user);
while(($file = readdir($dir)) !== false)  
{  
if($file !== '.' && $file !== '..' && !is_dir($file))  
{  
	$files[] = $file;  
}  
}  
closedir($dir);
natcasesort($files);
echo "<ul>\n";
for($i=0; $i<count($files); $i++)  
{
if($files[$i] != "index.php")
echo "<li><a href=\"$dir/".$files[$i]."\">".$files[$i]."</a> - <a href=\"delete.php?del=$files[$i]\">Delete</a></li>\n";
}
echo "</ul>\n";
?>

 

I know how to make a folder, I just want a way of modifying this so A) A folder icon shows, and B) when clicked, you can go into that folder and add to it.

Link to comment
https://forums.phpfreaks.com/topic/67764-image-uploader-help/
Share on other sites

Yello, just stumbled on your post and I'm in the process of building a fairly fully-fledged photo gallery for a custom CMS, thought I MIGHT be able to lend you a hand, but if my code comes out complete gibberish sorry, been in codeland wayyy too much lately :)

 

So anyway, just to kinda break this up a bit, sounds like you'll need the following to do what you need to.

 

1. Obviously a way for users to upload photos (which you have)

2. Some way of creating categories, folders, within that users' folder to store photos and possibly other folders.

 

on the flip side

 

A. Some way for users to retrieve photos

B. Some way for, thus, reading folder information, including, keeping grand infinity in mind :), the need for endless recursive searching.

 

 

So, first thing id say is create a way for your users' to create folders. And obviously if a user is gonna create a folder they need to have some idea of where they are in things, so you'll first need a function for displaying the current folder structure on their account. From there it's pretty much cheesecake.

 

After you've got the display, just make some buttons for adding more folders/images. Then no matter what folder they are in they can add new folders/images, navigate to those folders/images, etc.

 

And of course, if users are going to add photos/folders, their probably going to want to delete their photos/folders...

 

I was real, real tempted to just bust out the code for this stuff my self because I'd already written something similar but I learned a lot from writing it and I think you prolly will to, so not gonna drop any code in here cept for maybe snippets but anyway.

 

 

Well actually dang now I'm sitting here sorting this out in my head so ok ok, maybe some psuedo code and a function to ponder to get you started...

 

// Function for creating a folder
function create_a_folder($user, $folder_name) {
     // Get user info/user folder, open_dir
     // Drop in the folder
     // Redisplay so they see the change
}

// function for retrieving a folder
function get_folder_contents($user, $folder_name) {
     // Get user info
     // Grab the folder contents, if it exists
     // Redisplay so they can see the goods
}

// function for adding an image
function add_image($user, $folder, $file_info) {
     // Get user info
     // Drop image info into $folder, if it exists
     // Redisplay so they can see the goods
}

// function for deleting an image
function delete_image($user, $folder, $file_info) {
     // Get user info
 // Navigate to $folder within users folder
 // Drop the image that matches file_info
 // Redisplay so they can see the change
}

/*
Obviously at this point its plain to see there are some recurring elements
in the code design, at least in my solution to your code question, but as I
have no idea how the rest of your code is structured most of it is up in the
air, but anyway, touching on some OOP philosophy, it'd probably be best to
have a user object, loaded with the users information, that we can easily
retrieve the users information with. Folder names, username, etc. 

So you might have something like:
*/
class user {

function user($userid) {
	// Somehow grab your user information, dunno if your using a MySQL solution, flatfile, or what,
	// but by whatever method you choose to use, get user information on class initiation
}

function get_folder() {
	// Retrieve and return the users folder
}

// etc. etc.
}

 

 

So now i ALMOST feel like im rambling ;), but not quite so I'll continue hehehe :)

 

I guess basically what I'm getting at is if you want to start adding more functionality you're probably going to want to invest maybe half your time planning your code and structure, trust me, it'll save your sanity in the long run  :D

 

Anyway let me know if this helps, if it doesn't, if it seems off the wall, or what.

 

 

Pace,

- Be

Link to comment
https://forums.phpfreaks.com/topic/67764-image-uploader-help/#findComment-340586
Share on other sites

I can make folders, I know how to go about all that, the only problem is how to make the folders show up on the list, and how to make a function that means when they click on the folder in their area, it opens that folder and then displays the contents of it.

Link to comment
https://forums.phpfreaks.com/topic/67764-image-uploader-help/#findComment-340660
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.