Jump to content

Few problems with my code( creating multiplay files )


wladkov

Recommended Posts

Hello Everyone.

Im in the progress of creating a script that will create multiplay files at once, the script wiill do those things:

Will get the name of the file.

WIll get the source of the file.

Will put 777 chmods on it.

And it will save everything.

Ok so here's my code and my problems:

<html>
<body>
<head>
<title>Random File Saver</title>
</head>
<form method="post" action="">
<textarea name="nazwa">Name Of File</textarea> <br />
<textarea name="source" cols="40" rows="20">Enter text</textarea> <br />
<input type="submit" value="Create">
</form>
<?php
//variables
$filename = $_POST['nazwa'];
$filesource = stripslashes($_POST['source']);
//function create the file 
function create_file($filename, $filesource){
$fileh = fopen($filename, 'w+') or die("Cannot create a file");
fwrite($fileh, $filesource);
fclose($fileh);
chmod("$filename", 0777);
}
//use the function
create_file($filename, $filesource);
//if statment
if (create_file==true){
echo "<pre>File(s) that have been created: <br /><br /></pre>";
echo $filename;
}
?>

 

1. How can i prevent message "Cannot create a file" to show whenever the code is executed. Meaning it shows when i execute it once, then when i put the name and source there is no problem.

2. If you look at the last line, only the file name will print, is there a way that the user will be able to click on that file? Like in html using href option. Meaning i want some option that will read the file name from $_POST['nazwa'] and then it will allow the user to click on it.

3. Is there a way that i can upload many files at once? Like 5 files at once, or something and you can save each one with different name and source?

Okay , that would be all.

Thank you !

Link to comment
Share on other sites

1. How can i prevent message "Cannot create a file" to show whenever the code is executed. Meaning it shows when i execute it once, then when i put the name and source there is no problem.

 

you should prevent the code from executing unless a post has been sent

 

if(!isset($_POST['nazwa'])) exit;

 

2. If you look at the last line, only the file name will print, is there a way that the user will be able to click on that file? Like in html using href option. Meaning i want some option that will read the file name from $_POST['nazwa'] and then it will allow the user to click on it.

 

 echo "<a href=\"".$filename."">".$filename."</a>"; 

 

3. Is there a way that i can upload many files at once? Like 5 files at once, or something and you can save each one with different name and source?

 

Loops

 

Link to comment
Share on other sites

Okay guys, i added some things etc. I have a favor could you check those both codes and tell  me what you think about them. How could or should i improve them?

<html>
<head>
<title> File Delete </title>
</head>
<body bgcolor="black" text="red">
<center><pre><h2>  > . file delete . < </pre></h2>
<form method="post" action="">
<textarea name="delete">Delete File </textarea> <br />
<pre>Are you sure? </pre><br />
<input type="submit" value="Yes">
<input type="reset" value="No">
</form></center>
</body>
</html>
<?php

$filedelete = $_POST['delete'];
function delete_file($filedelete){
if(!isset($_POST['delete'])) exit;
unlink($_POST['delete']) or die("<pre>File couldn't be deleted.</pre>");
} 

delete_file($fieledelete);
if (delete_file==true) {
echo "<pre>File has been deleted.</pre>";
} 



?>

I know that if you execute this code without proper name it will show en error of Warning: unlink().... and then print "File couldnt be deleted";

How could i fix that? And is my if statment correct?

 

Also here is the second code:

<html>
<head>
<title>File Saver</title>
</head>
<body bgcolor="black" text="red">
<center><pre><h2>  > . file saver . < </pre></h2>
<form method="post" action="">
<textarea name="nazwa">Name Of File</textarea> <br />
<textarea name="source" cols="35" rows="20">Enter text</textarea> <br />
<input type="submit" value="Create">
</form></center>

<?php
//variables
$filename = $_POST['nazwa'];
$filesource = stripslashes($_POST['source']);
//function create the file 
function create_file($filename, $filesource){
if(!isset($_POST['nazwa'])) exit;
$fileh = fopen($filename, 'w+') or die("<pre>>>Cannot create a file.</pre>");
fwrite($fileh, $filesource);
fclose($fileh);
//uncoment to use chmods chmod("$filename", 0777);
}

//use the function
if (create_file==true){
create_file($filename, $filesource);
echo "<pre>>>File(s) that have been created: <br /><br /></pre>";
echo '<a href='.$filename.'>'.$filename.'<a/>';
}

?>

What you think is everything looks correct? How could i use loops to allow me put more than 1 file at the time and give him a name?

 

 

==

THANKS !!!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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