emvy03 Posted July 15, 2011 Share Posted July 15, 2011 Hi, I'm trying to write some code for a gallery where a user types a name into a box and submits it, which then creates a gallery page to hold images. So far I have written the code that takes the name typed in, creates a directory to hold the images and also creates a php file with the same name that is the actual gallery page. Using the fopen and fwrite functions I have got so far as writing the php gallery code, where a welcome line uses the gallery name. I have also written some code to display all the images in the given directory- this is where I am having issues, as the code doesn't use the name variable I have set and thus doesn't open the directory. Perhaps showing the code may give a better idea. <?php //Define the name variable $gall = mysql_real_escape_string($_POST['galleryname']); //Create a folder with the given name mkdir("gallery/$gall"); echo "Successfully Created. Go <a href='imageupload.php'>Back</a>"; ?> <?php //Create the gallery php file $ourFileName = "gallery/$gall.php"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fclose($ourFileHandle); ?> <?php //Write the gallery script to display images in given directory $myFile = "gallery/$gall.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "<h1>Welcome to the gallery $gall</h1>"; fwrite($fh, $stringData); $stringData = ' <?php //Take the gall variable and remove the .php suffix to give the directory $gall = $_POST["galleryselect"]; $final = substr($gall", 0, -4); if ($opendir = opendir($final)){ while(($file = readdir($opendir)) !== FALSE) { if($file!="."&&$file!=".."){ echo "<img id='dispimg' src ='$dir/$file'/><br>"; } } } ?> '; fwrite($fh, $stringData); fclose($fh); ?> Thanks for reading!! Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/ Share on other sites More sharing options...
AyKay47 Posted July 15, 2011 Share Posted July 15, 2011 Instead of doing what you are doing here, what I like to do to store/display images is to store the image path in my database under a specific gallery name like it looks like you have. Then when you display the images of a certain gallery, set the path value in your db to the img tag source in a while loop, to display all of the images from a specific gallery name..example $query = mysql_query("SELECT image_path FROM images WHERE gallery_name = '$gallery_name'"); while($row = mysql_fetch_array($query,MYSQL_ASSOC)){ $image_path = $row['image_path']; print "<img src='$image_path' /> <br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243185 Share on other sites More sharing options...
jcbones Posted July 15, 2011 Share Posted July 15, 2011 You shouldn't need to create separate pages for the galleries. Creating separate directories is fine, but you should be able to handle everything else on one page. Whether you use a flat file, or a database. So, say you make your gallery directories in a folder named gallery, which resides in the web_root directory, and your gallery.php page resides in the same directory. <?php define('GALLERY_DIRECTORY',str_replace('.php','/',$_SERVER['SCRIPT_FILENAME'])); //since this filename is gallery.php, and our gallery folder is named gallery, we strip the .php off the end of the file, and append a directory separator. $galleryName = strip_tags($_GET['gallery']); $gallery = GALLERY_DIRECTORY . $galleryName; if(!is_dir($gallery)) { die('Your gallery does not appear to exist!'); } echo 'You have found a photo gallery named: ' . $galleryName; Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243247 Share on other sites More sharing options...
emvy03 Posted July 15, 2011 Author Share Posted July 15, 2011 Hi, Thanks for your replies guys. With regards, to the mysql solution. I guess then that a new line would be created for a new image source. I'm assuming a new page is created for each gallery that grabs the gallery name? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243264 Share on other sites More sharing options...
xyph Posted July 15, 2011 Share Posted July 15, 2011 If MySQL is a little overwhelming right now, I can help you do this using a single flat file as a sort of database. Interested? Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243290 Share on other sites More sharing options...
emvy03 Posted July 16, 2011 Author Share Posted July 16, 2011 Cheers mate. I'm okish with mysql I think, I've managed to do a simple mysql databse with update, delete and insert functions so far but thank you all the same. I'm going to give the mysql code a go and see where I get with it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243407 Share on other sites More sharing options...
emvy03 Posted July 16, 2011 Author Share Posted July 16, 2011 Hi, I think I've written the mysql code but I'm having the same issue where the variable '$gallery_name' isn't being transferred (for want of a better word) so when the galley file is being written instead of writing in the gallery name it literally writes '$gallery_name'. The thing is though, when writing a title for the page it manages to grab the variable with no problems at all. Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243426 Share on other sites More sharing options...
dragon_sa Posted July 16, 2011 Share Posted July 16, 2011 Could you post the code you have now so we can try to find your issue Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243427 Share on other sites More sharing options...
emvy03 Posted July 16, 2011 Author Share Posted July 16, 2011 Here we are. (galleryname is the name of the textfield that the user writes the name into) <?php $gallery_name = mysql_real_escape_string($_POST['galleryname']); ?> <?php mkdir("gallery/$gallery_name"); //Create the gallery php file $ourFileName = "gallery/$gallery_name.php"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fclose($ourFileHandle); //Write the upload form script to the gallery php file $myFile = "gallery/$gallery_name.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "Welcome to '$gallery_name'"; fwrite($fh, $stringData); $stringData = ' <?php include "../storescripts/connect_to_mysql.php"; $query = mysql_query("SELECT image_path FROM images WHERE gallery_name = $gallery_name"); while($row = mysql_fetch_array($query,MYSQL_ASSOC)){ $image_path = $row["image_path"]; print "<img src=$image_path /> <br />"; } ?> '; fwrite($fh, $stringData); fclose($fh); ?> I'm thinking it must be something to do with the quote and double quotes in there but I don't know of a work around. :/ Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243430 Share on other sites More sharing options...
dragon_sa Posted July 16, 2011 Share Posted July 16, 2011 Hi give this a go and see if you get the desired results <?php $gallery_name = mysql_real_escape_string($_POST['galleryname']); $path="gallery/$gallery_name"; mkdir('$path', 0777); //Create the gallery php file $ourFileName = "$path.php"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fclose($ourFileHandle); //Write the upload form script to the gallery php file $myFile = "$path.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "Welcome to '$gallery_name'"; fwrite($fh, $stringData); $stringData.="<?php"; $stringData.="include('../storescripts/connect_to_mysql.php');"; $stringData.="$gallery=".$gallery_name; $stringData.="$query = mysql_query(\"SELECT image_path FROM images WHERE gallery_name = '$gallery'\");"; $stringData.="while($row = mysql_fetch_array($query,MYSQL_ASSOC)){"; $stringData.="$image_path = $row[\"image_path\"];"; $stringData.="print \"<img src=$image_path /> <br />\";"; $stringData.="}"; $stringData.="?>"; fwrite($fh, $stringData); fclose($fh); ?> I havent tested it Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243440 Share on other sites More sharing options...
jcbones Posted July 16, 2011 Share Posted July 16, 2011 Try this instead: <?php $gallery_name = mysql_real_escape_string($_POST['galleryname']); $path="gallery/$gallery_name"; mkdir('$path', 0777); //Create the gallery php file $ourFileName = "$path.php"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fclose($ourFileHandle); //Write the upload form script to the gallery php file $myFile = "$path.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "Welcome to '$gallery_name'"; fwrite($fh, $stringData); $stringData.='<?php'; $stringData.='include('../storescripts/connect_to_mysql.php');'; $stringData.='$gallery='.$gallery_name; $stringData.='$query = mysql_query("SELECT image_path FROM images WHERE gallery_name =\'' . $gallery . '\'");'; $stringData.='while($row = mysql_fetch_array($query,MYSQL_ASSOC)){'; $stringData.='$image_path = $row[\"image_path\"];'; $stringData.='print \"<img src=$image_path /> <br />\";'; $stringData.='}'; $stringData.='?>'; fwrite($fh, $stringData); fclose($fh); ?> Use single quotes around variables that you do NOT want the current script to parse, doubles around those that you do. Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243456 Share on other sites More sharing options...
dragon_sa Posted July 16, 2011 Share Posted July 16, 2011 I have tested this and I have corrected the issues works perfect now <?php $gallery_name = mysql_real_escape_string($_POST['galleryname']); $path="gallery/$gallery_name"; mkdir("$path", 0777); //Create the gallery php file $ourFileName = "$path/$gallery_name.php"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fclose($ourFileHandle); //Write the upload form script to the gallery php file $myFile = "$path/$gallery_name.php"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "Welcome to '$gallery_name'\n"; $stringData.="<?php\n"; $stringData.="include('../storescripts/connect_to_mysql.php');\n"; $stringData.="\$gallery=\"$gallery_name\";\n"; $stringData.="\$query=mysql_query(\"SELECT image_path FROM images WHERE gallery_name='\$gallery'\");\n"; $stringData.="while(\$row=mysql_fetch_array(\$query,MYSQL_ASSOC)){\n"; $stringData.="\$image_path=\$row[\"image_path\"];\n"; $stringData.="print \"<img src='\$image_path' /><br />\";\n"; $stringData.="}\n"; $stringData.="?>"; fwrite($fh, $stringData); fclose($fh); ?> absolutely correct about the single quotes as it ignores any escaped characters breaking the script, my bad originally Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243461 Share on other sites More sharing options...
dragon_sa Posted July 16, 2011 Share Posted July 16, 2011 one last edit change $stringData = "Welcome to '$gallery_name'\n"; to $stringData = "Welcome to '$gallery_name'<br />\n"; so the first image is not next to the heading text but under it Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243467 Share on other sites More sharing options...
emvy03 Posted July 16, 2011 Author Share Posted July 16, 2011 Cheers guys, that worked a treat. Just having a little trouble with the image upload. <?php function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; if(isset($_POST['Submit'])) { $image=$_FILES['image']['name']; if ($image) $filename = stripslashes($_FILES['image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo '<h1>Unknown extension!</h1>'; $errors=1; } else { $size=filesize($_FILES['image']['tmp_name']); $image_name=time().'.'.$extension; $gallery_name = $_POST['galleryselect']; $newname=$gallery_name.'/'.$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; }}}} if(isset($_POST['Submit']) && !$errors) { echo "Upload Successful."; } include "storescripts/connect_to_mysql.php"; $sql = mysql_query("INSERT into images (image_path) VALUES ($newname)"); ?> Keeps giving the error 'Unknown variable newname'. I've tested this code in a simpler system where the user just uploads a picture so I don't think there is a fundamental issue, just little nagging errors I think. Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243475 Share on other sites More sharing options...
AyKay47 Posted July 16, 2011 Share Posted July 16, 2011 1. check to make sure that $_POST['galleryselect'] is not empty by using empty 2. in your query, wrap $newname in single quotes ('') Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243503 Share on other sites More sharing options...
emvy03 Posted July 16, 2011 Author Share Posted July 16, 2011 Hi, thanks for your reply. I did both of your suggestions, but I'm still getting the same error. Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243505 Share on other sites More sharing options...
PFMaBiSmAd Posted July 16, 2011 Share Posted July 16, 2011 It would probably help if you posted the actual error message with the line number it is referring to ("Unknown variable" isn't exactly a one of the php error messages.) Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243507 Share on other sites More sharing options...
emvy03 Posted July 16, 2011 Author Share Posted July 16, 2011 Hi, Sorry guys. The code is (I changed it a little bit from the previous post): <?php function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; if(isset($_POST['Submit'])) { $image=$_FILES['image']['name']; if ($image) { $filename = stripslashes($_FILES['image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo '<h1>Unknown extension!</h1>'; $errors=1; } else { $size=filesize($_FILES['image']['tmp_name']); if ($size > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } $image_name=time().'.'.$extension; $gallery_name = ($_POST['galleryname']); $path="gallery/$gallery_name/"; $newname="$path".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; }}}} if(isset($_POST['Submit']) && !$errors) { echo "<h1>File Uploaded Successfully! Try again!</h1>"; } ?> <?php include "storescripts/connect_to_mysql.php"; $sql = mysql_query("INSERT into images (gallery_name,image_path) VALUES ('$gallery_name','$image_name')"); ?> Giving error message: Notice: Undefined variable: gallery_name in C:\Program Files\xampp\htdocs\ppyfc\addImageCode.php on line 69 Notice: Undefined variable: image_name in C:\Program Files\xampp\htdocs\ppyfc\addImageCode.php on line 69 Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243522 Share on other sites More sharing options...
emvy03 Posted July 16, 2011 Author Share Posted July 16, 2011 Solved, just moved the position of the variables Just need to hope the upload function works when the site goes live. Quote Link to comment https://forums.phpfreaks.com/topic/242078-using-the-fopen-function/#findComment-1243535 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.