Jump to content

create a file then write to it


MadnessRed

Recommended Posts

OK, I have an image upload script. Now I also want a file to be creates giving a name and description to the file. That can be done by creating the files

$img_path.'.txt'

and

$img_path.'_s.txt'

 

 

here is my code

//Wow, that worked, but its just a file. We need to add the users information otherwise the gallery will look very boring.
//We need to see if the file exists again.
//What was the images path and name
$img_path = '/Images/'.$category.'/'.$name.'_'.$filename;

if (file_exists($img_path))
{
echo "File already exists, no comments made";
}
else
{
//So now lets get the name and desciption
$sdesc_i = $_POST["sdesc"];
$desc = $_POST["desc"];

//Lets brand the name
$sdesc = $sdesc_i.' by '.$name;

//Lets make the description files
//List the files to be written to

$file_sdesc = $img_path.'_s.txt';

$file_desc = $img_path.'.txt';


//Create the file and write the text to it

$handle_sdesc = fopen($file_sdesc, 'w');

fwrite($handle_sdesc, $sdesc);

$handle_desc = fopen($file_desc, 'w');

fwrite($handle_desc, $desc);


//So lets summarise the New file info
echo '<br /><hr /><br />';
echo 'File name :';
echo $sdesc;
echo '<br /><br />';
echo 'Description:';
echo $desc;
}

 

And here are the errors

 

Warning: fopen(/Images/MM2/Anthony_test.jpg_s.txt): failed to open stream: No such file or directory in /home/www/madnessred.co.cc/uploadfile.php on line 96

Warning: fwrite(): supplied argument is not a valid stream resource in /home/www/madnessred.co.cc/uploadfile.php on line 97

Warning: fopen(/Images/MM2/Anthony_test.jpg.txt): failed to open stream: No such file or directory in /home/www/madnessred.co.cc/uploadfile.php on line 99

Warning: fwrite(): supplied argument is not a valid stream resource in /home/www/madnessred.co.cc/uploadfile.php on line 100

 

and here are the lines they are refering too

 

	$handle_sdesc = fopen($file_sdesc, 'w');

fwrite($handle_sdesc, $sdesc);

$handle_desc = fopen($file_desc, 'w');

fwrite($handle_desc, $desc);

 

From what I can tell it is saying that the file doesn't exist so it can't do anything but at php.net it says

 

'w'

Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

 

Any ideas, the directory I want hte file to be create din is chmodded to 777

Link to comment
https://forums.phpfreaks.com/topic/110991-create-a-file-then-write-to-it/
Share on other sites

1) Database "code" is very easy to learn.  There's a million and a half tutorials.

 

2) It won't be much rewriting, honestly. O_O

 

3) Don't use a free host?  =/  99.99% of paid hosts have some sort of MySQL database available with a PHP package.

 

Files are messy, they can be error-prone (permissions...), and they just waste space, honestly. =/  2 files for every image?  So if you had 100 images, that's TWO HUNDRED extra files to sort through when you could just use a database.

ok, I will endeavor to learn sql over the hols. meanwhile though,

 

can you tell me how I would go about it as an sql database though

 

I have already edited the gallery slightly to allow a description

 

 

From

$desc_file = $_GET[entry].".txt";
$desc_exists = file_exists($desc_file);
if ($desc_exists) {
$handle = fopen($desc_file, "r");
if (!feof($handle)) {
   		$desc = fgetss($handle, 1024);
  	}
fclose($handle);
}else{
$desc = "No Description";
}

// Do our template Replaces
$html_out = str_replace("<!--LinkBack//-->", "<a href=\"$_SERVER[HTTP_REFERER]\">Back</a>", $html_out);
$html_out = str_replace("<!--Modified//-->", date ("F d Y H:i:s.", filemtime($_GET[entry])), $html_out);
$html_out = str_replace("<!--Filename//-->", basename($_GET[entry]), $html_out);
$html_out = str_replace("<!--Description//-->", $desc, $html_out);
$html_out = str_replace("<!--Image//-->", "<img src=\"$_GET[entry]\" width=770>", $html_out);
$html_out = str_replace("<!--VersionFooter//-->", $config['version'], $html_out);
echo $html_out;

 

 

To

$desc_file = $_GET[entry].".txt";
$desc_exists = file_exists($desc_file);
if ($desc_exists) {
$handle = fopen($desc_file, "r");

if (!feof($handle)) {
   		$desc = fgetss($handle, 1024);
  	}
fclose($handle);
}else{
$desc = "No Description";
}
$sdesc_file = $_GET[entry]."_s.txt";
$sdesc_exists = file_exists($sdesc_file);

if ($sdesc_exists) {
$handle = fopen($sdesc_file, "r");
if (!feof($handle)) {
   		$sdesc = fgetss($handle, 1024);
  	}
fclose($handle);
}else{
$sdesc = basename($_GET[entry]);
}

// Do our template Replaces
$html_out = str_replace("<!--LinkBack//-->", "<a href=\"$_SERVER[HTTP_REFERER]\">Back</a>", $html_out);
$html_out = str_replace("<!--Modified//-->", date ("F d Y H:i:s.", filemtime($_GET[entry])), $html_out);
$html_out = str_replace("<!--Filename//-->", $sdesc, $html_out);
$html_out = str_replace("<!--Description//-->", $desc, $html_out);
$html_out = str_replace("<!--Image//-->", "<img src=\"$_GET[entry]\" width=770>", $html_out);
$html_out = str_replace("<!--VersionFooter//-->", $config['version'], $html_out);
echo $html_out;

 

How would I get that to look up the 2 bits of data from an SQL

 

and how would I get the script earlier to write to the database?

i don't really want a new 1 as the one i have at the moment works fine except from the small problem i posted about and that is me not the script.

 

could you jsut say what is wrong with the script, or more to the point, hw you would go about creating a file then writing to it?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.