Posting & Upload
Posted: Sat Jun 25, 2005 7:55 pm
G'day,
I'm a php newbie and I need some help with a page that has a form for a user to upload files and at the same time enter some text into text fields and save that text to a txt file. I'm doing this in order to post an image and a description that image. I have the page that displays the image and description but I am having problems with the posting page.
Basically when the user selects the image from his local drive and enters the description I need to have the description saved in a txt file with the same name as the image: ex - filename.jpg & filename.txt.
Here's what I have so far, a message post script and an upload script. The upload works, as does the posting....however the posting portion save the file name as 1.txt, 2.txt, etc.....and posts when the user hits the page.....Any ideas???
Thnx in advance!
JCART | Please use
I'm a php newbie and I need some help with a page that has a form for a user to upload files and at the same time enter some text into text fields and save that text to a txt file. I'm doing this in order to post an image and a description that image. I have the page that displays the image and description but I am having problems with the posting page.
Basically when the user selects the image from his local drive and enters the description I need to have the description saved in a txt file with the same name as the image: ex - filename.jpg & filename.txt.
Here's what I have so far, a message post script and an upload script. The upload works, as does the posting....however the posting portion save the file name as 1.txt, 2.txt, etc.....and posts when the user hits the page.....Any ideas???
Thnx in advance!
Code: Select all
<?php
//POSTING SCRIPT
$address = $_POST['address'];
$description = $_POST['description'];
$date = date("F j, Y", time());
if ( ! is_dir("files/") )
{
mkdir("files/");
}
if ( $handle = opendir("files/") )
{
while ( ( $file = readdir( $handle ) ) !== false )
{
if ( $file != "." && $file != ".." )
{
$files_array[] = $file;
}
}
closedir( $handle );
}
$num_files = ( count( $files_array ) + 1 );
touch("files/$num_files.txt");
$final = "$date\n$address\n$description";
$fp = fopen("files/$num_files.txt", "w");
flock( $fp, LOCK_EX );
fputs( $fp, $final );
flock( $fp, LOCK_UN );
fclose( $fp );
echo '<div align="left"><p class="content">Your news article has been posted!</p></div>';
//UPLOAD SCRIPT
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$upload_dir = "files/";
$upload_url = $url_dir."/files/";
$message ="";
//create upload_files directory if not exist
//If it does not work, create on your own and change permission.
if (!is_dir("files")) {
die ("upload_files directory doesn't exist");
}
if ($_FILES['userfile']) {
$message = do_upload($upload_dir, $upload_url);
}
else {
$message = "Invalid File Specified.";
}
print $message;
function do_upload($upload_dir, $upload_url) {
$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];
$file_type = $_FILES['userfile']['type'];
$file_size = $_FILES['userfile']['size'];
$result = $_FILES['userfile']['error'];
$file_url = $upload_url.$file_name;
$file_path = $upload_dir.$file_name;
//File Name Check
if ( $file_name =="") {
$message = "Please specify the file";
return $message;
}
//File Size Check
else if ( $file_size > 50000000) {
$message = "The file size is over 50MB.";
return $message;
}
//File Type Check
else if ( $file_type == "text/plain" ) {
$message = "Sorry, You cannot upload any script file" ;
return $message;
}
$result = move_uploaded_file($temp_name, $file_path);
$message = ($result)?"File url <a href=$file_url>$file_url</a>" :
"Somthing is wrong with uploading a file.";
return $message;
}
?>
<html>
<head>
<title>upload and post</title>
</head>
<body>
<form method="post" ENCTYPE="multipart/form-data" name="upload" target="_self" id="upload">
<table width="37%" border="0" cellspacing="10" cellpadding="0">
<tr>
<td width="26%" valign="top"><div align="right">Address:</div></td>
<td width="74%"><textarea name="address" cols="25" rows="4" id="address"></textarea></td>
</tr>
<tr>
<td valign="top"><div align="right">Features:</div></td>
<td><textarea name="description" cols="25" rows="8" id="description"></textarea></td>
</tr>
<tr>
<td valign="top"><div align="right">Upload PDF: </div></td>
<td><input type="file" id="userfile" name="userfile"></td>
</tr>
<tr>
<td> </td>
<td><input name="upload" type="submit" value="Upload & Post"></td>
</tr>
</table>
<p><a href="files/" target="_blank" class="style1">--GO TO UPLOADS--</a>
</p>
</form>
</body>
</html>Code: Select all
tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]