Help with Image Uploading/Directory Creation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tsm4781
Forum Commoner
Posts: 38
Joined: Wed Jul 09, 2003 7:17 pm

Help with Image Uploading/Directory Creation

Post by tsm4781 »

I am trying to build something that is a little out of my league, but want to learn to do it so I've come here. I have a site where when users sign up, they can post certain information on the site and it displays under their unique user ID.

I am trying to put a build into the site where users can upload images. What I'd like it to do is upon initial upload, the PHP script looks for a directory name within /home/sitename/public_html/users called $username. If there is no directory, it should create one by locating their their unique ID from the database, $username, and making the directory. Then the user can upload images into that folder, and also via the database store the filename, a description, title, author, and genre. I have a simple upload script that I am using, but it is no where near locked down per user, and it doesn't allow for the "function MakeDirectoryName ($username)"

Attached is the code that I have for the simple upload. This is being built for an already custom PHP site, so I don't really want to integrate an image content management system into it as I have specific administration systems running for client and admin. Any help as to where scripts may be that does this, or your help with helping me figure this out will be extremely appreciated.

<html>
<head>
<title>Viewing Files in a Directory</title>
<link rel="stylesheet" href="../style.css" type="text/css">
</head>
<body>
<table border=0 width="60%" cellspacing=1 cellpadding=3 bgcolor=cccccc align=center>

Code: Select all

<?php
if ($Upload) { // Handle File Uploads.
	print ("<tr bgcolor=ffffff><td colspan=5 align=center>Uploaded file name: $File_name</td></tr>\n");
	print ("<tr bgcolor=ffffff><td colspan=5 align=center>Uploaded file size: $File_size</td></tr>\n");
	if (copy ($File, "users/$File_name")) {
		print ("<tr bgcolor=ffffff><td colspan=5 align=center>Your file, $File_name, was successfully uploaded!</td></tr>\n");
	} else {
		print ("<tr bgcolor=ffffff><td colspan=5 align=center>Your file, $File_name, could not be copied.</td></tr>\n");
	}
	unlink ($File);
	print ("<tr bgcolor=ffffff><td colspan=5 align=center>&nbsp;</td></tr>\n");
}

if ($Delete) { // Handle file deletions.
	for ($i = 0; $i < count ($Delete); $i++) {
		if ( unlink ("users/$Delete[$i]") ) {
			print ("<tr bgcolor=ffffff><td colspan=5 align=center>Your file, $Delete[$i], was successfully deleted!</td></tr>\n");
		} else {
			print ("<tr bgcolor=ffffff><td colspan=5 align=center>Your file, $Delete[$i], could not be deleted.</td></tr>\n");
		}
	}
	print ("<tr bgcolor=ffffff><td colspan=5 align=center>&nbsp;</td></tr>\n");
}

if ($Rename) { // Handle file renaming.
	for ($n = 0; $n < count ($Rename); $n++) {
		$OldFilename = $Rename[$n];
		$Old = "users/$OldFilename";
		$New = "users/$NewName[$OldFilename]";
		if ( rename ($Old, $New) ) {
			print ("<tr bgcolor=ffffff><td colspan=5 align=center>Your file, $Rename[$n], was successfully renamed!</td></tr>\n");
		} else {
			print ("<tr bgcolor=ffffff><td colspan=5 align=center>Your file, $Rename[$n], could not be renamed.</td></tr>\n");
		}
	}
	print ("<tr bgcolor=ffffff><td colspan=5 align=center>&nbsp;</td></tr>\n");
}

// Start Form

print ("<FORM ACTION="files.php" method=post enctype="multipart/form-data">\n");
print ("<tr bgcolor=ffffff><td><b>Image</b></td><td><b>File Name</b></td><td><b>File Size</b></td><td><b>Delete</b></td><td><b>Rename</b> (Enter the New Name in the box)</td></tr>\n");

// Read the files from the directory.

$Open = opendir ("users");
while ($Files = readdir ($Open)) {
	$Filename = "users/" . $Files;
	if (is_file ($Filename)) {
		$Size = filesize ("users/$Files");
		print ("<tr bgcolor=ffffff><td><img src="http://www.iyoy.com/test/users/$Files" width="150"></td><td><a href="http://www.iyoy.com/test/users/$Files">$Files</a></td><td>$Size</td><td><input type=checkbox name="Delete[]" value="$Files"></td><td><input type=checkbox name="Rename[]" value="$Files"><input type=text name="NewName[$Files]"></td></tr>\n");
	}
}
closedir ($Open);

// Give the upload option.

print ("<tr bgcolor=ffffff><td colspan=5 align=center>&nbsp;</td></tr>\n");
print ("<tr bgcolor=ffffff><td colspan=5 align=center><input type=checkbox name="Upload" value="Yes">Upload a file to the server:<input type=file name="File" size=20></td></tr>\n");
print ("<tr bgcolor=ffffff><td colspan=5 align=center><input type=submit name="submit" value="Submit!"></form></td></tr>\n");
?>
</table>
</body>
</html>

Thanks!
tsm4781
Forum Commoner
Posts: 38
Joined: Wed Jul 09, 2003 7:17 pm

Post by tsm4781 »

can anyone help me with this?
Post Reply