ballhogjoni Posted June 23, 2007 Share Posted June 23, 2007 Say I want to add a file named 123.php to the folder /xxxx/ How would I do that using php and a form? Is there a function or a tutorial? Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/ Share on other sites More sharing options...
mkosmosports Posted June 23, 2007 Share Posted June 23, 2007 You could use the file_put_contents function, so for example: $filename = "xxxx/123.php"; $filecontent = "Contents of file"; $createfile = file_put_contents($filename,$filecontents) or die("Cannot create file"); Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280867 Share on other sites More sharing options...
chigley Posted June 23, 2007 Share Posted June 23, 2007 <?php $handle = fopen("/xxxx/123.php", "w") or die("Failed to create /xxxx/123.php, check the file's CHMOD permissions."); fclose($handle); ?> Simple as that really. Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280868 Share on other sites More sharing options...
ballhogjoni Posted June 23, 2007 Author Share Posted June 23, 2007 do I have to put the /xxxx/ in the $filename variable or can I just put the 123.php in the $filename variable? Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280870 Share on other sites More sharing options...
mkosmosports Posted June 23, 2007 Share Posted June 23, 2007 You would have to put the full path... Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280874 Share on other sites More sharing options...
ballhogjoni Posted June 23, 2007 Author Share Posted June 23, 2007 gotcha... Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280875 Share on other sites More sharing options...
ballhogjoni Posted June 23, 2007 Author Share Posted June 23, 2007 I am having this problem:Fatal error: Call to undefined function: file_put_contents() in /home/cccccc/public_html/ccdfcdfd/popular_cards/index.php on line 13 this is my code <?php $filename = $_POST['file_name']; $filecontent = $_POST['filecontent']; if (!empty($filecontent) && !empty($filename)) { include('config.php'); $query = mysql_query("SELECT * FROM popular_cards") or die(mysql_error()); while ($row = mysql_fetch_array($query)) { echo "ID: ".$row["ID"]." & the code is: ".$row["code"]."<br>"; } $createfile = file_put_contents($filename,$filecontents) or die("Cannot create file"); // problem line $query1 = "INSERT INTO popular_cards (code) VALUES ('$filecontent')"; mysql_query($query1); mysql_close(); /*unset($filecontent); unset($filename);*/ } ?> Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280930 Share on other sites More sharing options...
chigley Posted June 23, 2007 Share Posted June 23, 2007 file_put_contents() is PHP5 compatible only. Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280932 Share on other sites More sharing options...
ballhogjoni Posted June 23, 2007 Author Share Posted June 23, 2007 crap... is ther any thing for 4.0 or higher? Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280934 Share on other sites More sharing options...
chigley Posted June 23, 2007 Share Posted June 23, 2007 Look at my first post in this thread! Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280936 Share on other sites More sharing options...
ballhogjoni Posted June 23, 2007 Author Share Posted June 23, 2007 I am not trying to open a file I am trying to create one. Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280944 Share on other sites More sharing options...
chocopi Posted June 23, 2007 Share Posted June 23, 2007 maybe you should look at THE MANUAL and look at what the "w" does ~ Chocopi Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280945 Share on other sites More sharing options...
ballhogjoni Posted June 23, 2007 Author Share Posted June 23, 2007 Thanks I just did. It only writes to the file if it has been created already. Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280946 Share on other sites More sharing options...
ballhogjoni Posted June 23, 2007 Author Share Posted June 23, 2007 Looking for something like file_create() Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280949 Share on other sites More sharing options...
chocopi Posted June 23, 2007 Share Posted June 23, 2007 '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. I think you must have missed this bit ~ Chocopi Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280951 Share on other sites More sharing options...
ballhogjoni Posted June 23, 2007 Author Share Posted June 23, 2007 Thank you it worked. ;D Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280957 Share on other sites More sharing options...
chigley Posted June 23, 2007 Share Posted June 23, 2007 My code works... trust me Link to comment https://forums.phpfreaks.com/topic/56844-solved-how-do-i-add-a-new-file-to-a-folder/#findComment-280976 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.