Jump to content

Using the fopen function


emvy03

Recommended Posts

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!!

Link to comment
Share on other sites

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 />";
}

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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. :/

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.