APD1993 Posted May 29, 2012 Share Posted May 29, 2012 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. Quote Link to comment Share on other sites More sharing options...
scootstah Posted May 29, 2012 Share Posted May 29, 2012 Use file_put_contents to create the file. Quote Link to comment Share on other sites More sharing options...
APD1993 Posted May 29, 2012 Author Share Posted May 29, 2012 Use file_put_contents to create the file. No luck for me when I tried that (used it in place of fputs) Quote Link to comment Share on other sites More sharing options...
scootstah Posted May 29, 2012 Share Posted May 29, 2012 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")); Quote Link to comment Share on other sites More sharing options...
APD1993 Posted May 29, 2012 Author Share Posted May 29, 2012 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 Quote Link to comment Share on other sites More sharing options...
scootstah Posted May 29, 2012 Share Posted May 29, 2012 Anywhere really. Here is good: <?php var_dump(is_writable("C:/xampp/htdocs/RCM3")); $filename=$_POST['filename']; Quote Link to comment Share on other sites More sharing options...
APD1993 Posted May 29, 2012 Author Share Posted May 29, 2012 Anywhere really. Here is good: <?php var_dump(is_writable("C:/xampp/htdocs/RCM3")); $filename=$_POST['filename']; I got bool(true) Quote Link to comment Share on other sites More sharing options...
scootstah Posted May 29, 2012 Share Posted May 29, 2012 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? Quote Link to comment Share on other sites More sharing options...
APD1993 Posted May 29, 2012 Author Share Posted May 29, 2012 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. Quote Link to comment Share on other sites More sharing options...
scootstah Posted May 30, 2012 Share Posted May 30, 2012 Because like I said, you are only checking $filename. You should be checking the full path, IE: C:/xampp/htdocs/RCM3/$filename Quote Link to comment Share on other sites More sharing options...
APD1993 Posted May 30, 2012 Author Share Posted May 30, 2012 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 Quote Link to comment Share on other sites More sharing options...
scootstah Posted May 30, 2012 Share Posted May 30, 2012 Why not? You already know what the file is called, and where it is stored. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.