Page 1 of 1

Php newb needs some simple help...

Posted: Wed Jun 13, 2007 2:31 am
by quilby
Ok I am currently using this upload script that I found:

Code: Select all

<?
	include("uploader.php");
?>
<header>
<title>Upload scripts to iScripts</title>
</header>
		<p><strong><span style="background: #fff; color: #000"><? if($_REQUEST["message"] == "") echo "Upload a file below:"; else echo $_REQUEST["message"]?></span></strong></p>
		<form action="upload.php" enctype="multipart/form-data" id="upload" method="post">
			<p><input id="userfile" name="userfile" size="45" type="file" /><input name="upload" type="submit" value="Upload File" /><br /></p>

			<p>Allowed file extensions: <strong><?=$file_extensions_list?></strong></p>

			<p>Maximum file size: <strong><?=$maximum_file_size?> bytes (~<?=round($maximum_file_size/1024)?>KB)</strong></p>

			<p>Deleting files is currently: <strong><?=$status?></strong></p>

                        
			
		</form>

		<p><strong>Uploaded Files</strong></p>
		<table style="border: 2px dotted #000; width: 100%">
	<? if($uploaded_files == "") echo "		<tr>
				<td colspan=\"2\" style=\"background: #fff; color: #000; text-align: center\"><br /><strong>There are no uploaded files.</strong><br /><br /></td>
			</tr>
	"; else echo $uploaded_files ?>
	</table>

Code: Select all

<?
	/*
	Silentum Uploader v1.2.0
	Modified March 4, 2007
	uploader.php copyright 2005-2007 "HyperSilence"
	*/

	// Begin options

	$allow_file_deletion = false; // To allow visitors to delete files, leave this at true; otherwise, change it to false

	$file_extensions = array(".java"); // Add or delete the file extensions you want to allow

	$file_extensions_list = ".java"; // Type the same as above, without the quotes separating them

	$max_length = 30; // The maximum character length for a file name

	$maximum_file_size = "1000000"; // In bytes

	$upload_log_file = "uploadlog.txt"; // Change this to the log file you want to use

	// End options

	$folder_directory = "http://".$_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"]);
	$message = "";
	$set_chmod = 0;
	$site_uri = "http://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"];
	$upload_directory = "files/";
	$upload_uri = $folder_directory."/files/";

	if($allow_file_deletion == true) $status = "enabled";
	else $status = "disabled";

	if($_REQUEST["delete"] && $allow_file_deletion) {
	$resource = fopen($upload_log_file,"a");
	fwrite($resource,date("F d, Y / h:i:sa")." - ".$_REQUEST["delete"]." deleted by ".$_SERVER["REMOTE_ADDR"]."\n");
        fclose($resource);

	if(strpos($_REQUEST["delete"],"/.") > 0);
	elseif(strpos($_REQUEST["delete"],$upload_directory) === false);
	elseif(substr($_REQUEST["delete"],0,6) == $upload_directory) {
	unlink($_REQUEST["delete"]);
	$message = "File has been deleted.";
	header("Location: $site_uri?message=$message");
	}
	}

	elseif($_FILES["userfile"]) {
	$resource = fopen($upload_log_file,"a");
	fwrite($resource,date("F d, Y / h:i:sa")." - ".$_FILES["userfile"]["name"]." "
	.$_FILES["userfile"]["type"]." uploaded by ".$_SERVER["REMOTE_ADDR"]." \n");
	fclose($resource);

	$file_type = $_FILES["userfile"]["type"]; 
	$file_name = $_FILES["userfile"]["name"];
	$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
	//chmod($upload_uri."".$file_name, 0755);
	if($_FILES["userfile"]["size"] > $maximum_file_size) {
	$message = "ERROR: File size cannot be over ".$maximum_file_size." bytes.";
	}

	elseif($file_name == "") $message = "ERROR: Please select a file to upload.";
	elseif(strlen($file_name > $max_length)) $message = "ERROR: The maximum length for a file name is ".$max_length." characters.";
	elseif(!preg_match("/^[A-Z0-9_.\- ]+$/i",$file_name)) $message = "ERROR: Your file name contains invalid characters.";
	elseif(!in_array($file_ext, $file_extensions))
	$message = "ERROR: <ins>$file_ext</ins> is not an allowed file extension.";
	else $message = upload_file($upload_directory, $upload_uri);
	//header("Location: $site_uri?message=$message");
	}

	elseif(!$_FILES["userfile"]);
	else $message = "ERROR: Invalid file specified.";

	$open = opendir($upload_directory);
	$uploaded_files = "";
	while($file = readdir($open)) {
	if(!is_dir($file) && !is_link($file)) {
	$uploaded_files .= "		<tr>
				<td style=\"background: #fff; color: #000; text-align: left; width: 70%\"><a href=\"$upload_directory$file\" title=\"$file (".filesize($upload_directory."".$file)." bytes)\">".$file."</a> (".filesize($upload_directory."".$file)." bytes)</td>";
	if($allow_file_deletion)
	$uploaded_files .= "
				<td style=\"background: #fff; color: #000; text-align: right; width: 30%\"><a href=\"?delete=$upload_directory".urlencode($file)."\" title=\"Delete File\">Delete File</a></td>";
	else
	$uploaded_files .= "
				<td style=\"background: #fff; color: #000; text-align: right; width: 30%\"><del><strong>Delete File</strong></del></td>";
	$uploaded_files .= "
			</tr>
			<tr>
				<td colspan=\"2\" style=\"background: #eee; color: #000; text-align: left; text-indent: 20px\">Uploaded <strong>".date("F d, Y / h:ia", filemtime($upload_directory.$file))."</strong></td>";
	$uploaded_files .="
			</tr>
	";
	}
	}

	function upload_file($upload_directory, $upload_uri) {
	$file_name = $_FILES["userfile"]["name"];
	$file_name = str_replace(" ","_",$file_name);
	$file_path = $upload_directory.$file_name;
	$temporary = $_FILES["userfile"]["tmp_name"];

	$result = move_uploaded_file($temporary, $file_path);
	if(!chmod($file_path,0777))
	$message = "ERROR: A folder to place the files was not found, or the files need to be CHMODed to 777.";
	else $message = ($result)?"File has been uploaded." : "An error has occurred.";
	return $message;
	}
?>
I want to add to it a field where the uploader can also leave a comment. The comment will then be made into a text file and saved in the 'files' directory. The name of the text file will be just like the name of the file + comment. So for example if I upload a file called hello.java the comment file will be helloComment.txt and both files will be saved in the files directory. Then, last but not least, I want to have a .php page that will list all the files that are currently in the files directory, and then next to each file name it will echo the .txt comment file.

Thanks!

Posted: Wed Jun 13, 2007 3:10 am
by Rovas
I don' t understand what you want to do: upload another file or just put a text attached to the file? Why have you put the entire code put what you want to modify or that gives you errors.
You need to make a function/ class to make the uploads if you want more to upload more then 1 file and you called it for every file the user uploads using the form. Or you can make a database table that contains the files uploaded by user and the comments he made to them (the kind Flickr uses).
If you want to make a file containing the comments made by the uploader I suggest you don' t.
Take a look here http://www.php.net/manual/en/features.file-upload.php or do a search for similar posts on this forum.

Posted: Wed Jun 13, 2007 4:15 am
by quilby
Maybe it will be easier to understand what I want if I tell you what I need it for. I made a java program that downloads all the files in a certain directory of a website to my computer. Now, how does it know the names of the files? It goes to the uploadlog.txt of the server which looks like this:
June 13, 2007 / 07:07:59am - A.java text/x-java uploaded by 37.68.181.111
June 13, 2007 / 07:07:60am - Hello.java text/x-java uploaded by 37.68.181.111
June 13, 2007 / 07:08:01am - Bob.java text/x-java uploaded by 37.68.181.111
June 13, 2007 / 07:08:05am - Something.java text/x-java uploaded by 37.68.181.111
June 13, 2007 / 07:09:43am - ClassB.java text/x-java uploaded by 37.68.181.111
So the program reads that file and then it knows that names of the files that it needs to download. But the uploadlog.txt file isnt good enough. It does not change if a file gets deleted. And then the program screws up. Also, if someone want to upload a file and also include a comment that needs to be seen by the program how are they gonna do that. So what I want to do is change the upload form so that when you upload a file you can also include a comment. Then I will also replace the uploadlog.txt with a dynamic php file, that will check to see what files it can find in the files directory. It will display all the file names and then next to each file it will display the comment that the uploader wanted to include. Thats it. Its not that much. I just dont know how to do it. Maybe I could also, like you said, use a mysql db to store the comments.