Posting & Upload

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
plankguy
Forum Newbie
Posts: 18
Joined: Sat Jun 25, 2005 7:39 pm

Posting & Upload

Post by plankguy »

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!

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>&nbsp;</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>
JCART | Please use

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]
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Post by robjime »

Its posting when the user hits the page because the script doesn't have anything stoping it from doing so at the begging. Try:

Code: Select all

if(isset($_POST['upload'])) { //the whole script here 
} else {
echo //html; 
}
plankguy
Forum Newbie
Posts: 18
Joined: Sat Jun 25, 2005 7:39 pm

Post by plankguy »

I've tried what you suggested.....doesn't seem to post or upload at all anymore....It does surpress the post when the user hits the page tho. :wink:
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Please change code TAGS into php TAGS for better reading :)

Code: Select all

<form name="upload" target="<?php echo $_SERVER['PHP_SELF'];?>" id="upload" method="post" enctype="multipart/form-data">
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

...

Post by s.dot »

Seems like it'd be much easier to just store the filename and description in a DB.

Then call them to the page.
plankguy
Forum Newbie
Posts: 18
Joined: Sat Jun 25, 2005 7:39 pm

Post by plankguy »

My client's host doesn't support DB....I wish.
Post Reply