Jump to content

How to get files to be created by users and uploaded to a folder?


APD1993

Recommended Posts

Hello,

 

I am currently working on a basic content management system, one that allows for the user to enter in a file name in a text box and the contents of the file in another text box (fileupload.html). I would like it to be working so that when I click to submit this information, a file is created and stored in a folder on my computer called RCM3 by going through the fileupload.php script. However, whenever I try to create a file (e.g. enter a name and the contents in the text boxes), I get shown the "Unable to create file" message.

 

The coding for my fileupload.html form is as follows:

 

<html><head><title>File Uploader Page</title></head>
<body>
<h3>File Uploader</h3>
<form action="fileupload.php" method="post" enctype="multipart/form-data">
File Name:<br>
<input type="text" size="45" name="filename"><br>
Extra Information:<br>
<input type="text" name="info">
<input type="submit" value="Create File">
</form>
</body>
</html>

 

And my coding for fileupload.php at the moment is:

 

<?php
$filename=$_POST['filename'];
$info=$_POST['info'];
$file=fopen("C:/xampp/htdocs/RCM3/".$filename, "w");
fputs($file, $info);
fclose($file);
?>

<html><head><title>Creating A File</title></head>
<body>
<?php
if(file_exists($filename)){
$file_length=filesize($filename);
$msg="File created: $filename";
$msg.="File size:$file_length bytes";
echo($msg);
}
else
{
echo("Unable to create file");
}
?>
</body>
</html>

 

Any help is appreciated.

Link to comment
Share on other sites

Oops, I didn't even see the part of the code with the fputs.

 

Do you get any errors? Do you have write permissions?

 

What does this give you?

var_dump(is_writable("C:/xampp/htdocs/RCM3"));

 

Whereabouts would I place that code?

 

I'm not sure that it's a permission issue either

Link to comment
Share on other sites

I think the issue is that you are not checking that the file exists in the same path as the one you are saving it to. You are checking to see if $filename exists in the same directory that the script is run.

 

Have you checked if the files are actually created or not by viewing the directory?

Link to comment
Share on other sites

I think the issue is that you are not checking that the file exists in the same path as the one you are saving it to. You are checking to see if $filename exists in the same directory that the script is run.

 

Have you checked if the files are actually created or not by viewing the directory?

 

I just checked, and it does seem to be adding them to the folder. The thing that's confusing me now then is why it's saying that the file cannot be created when it has been created.

Link to comment
Share on other sites

Because like I said, you are only checking $filename. You should be checking the full path, IE: C:/xampp/htdocs/RCM3/$filename

 

That's the thing though, the files are being created dynamically, so I can't really check for a specific filename at that point

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.