Jump to content

A few problems...


calvin182

Recommended Posts

Two problems...PROBLEM A:Basically, this is the page that my upload form is sent to. What I need it to do is load the array (db.php), upload the image and rename it if needed, add the image to the array, sort the array, and then rebuild the array so it may be saved into a separate file (db.php). It does everything but the last step properly. The problem is it will add the new entry into the array and save it just fine, but if the array contains existing values, it does not add them to the saved version.

<?phpinclude('db.php');$db = array();$uploaddir = $_POST['uploaddirectory'];$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);$path_parts = pathinfo($uploadfile);$filename = $path_parts['basename'];//$fileext = "." . substr(strrchr($filename, "."), 1);// $filename = basename($uploaddir, $fileext);$today = date("m-d-Y_H-i-s");if (file_exists($uploadfile)) {   $file = $today . $filename;} else {   $file = $filename;};$uploadfile2 = $uploaddir . $file;if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile2)) {     chmod($uploadfile2, 0777);     echo "File was successfully uploaded!"; $backmessage = "Upload another";                                                          $addtodb = "true";  } else {  echo "Upload failed!"; $backmessage = "Back to upload";}echo "<br/><a href=\"add.php\" title=\"$backmessage\">$backmessage</a>";if ($addtodb == "true") {     $newentry = "$db['".$today."'] = \"".$file."\";";} else {exit(0);};krsort($db);$output = "$db = array(); " . $newentry;foreach ($db as $key => $value) {     $output .= " $db['".$key."'] = \"".$value."\";";}$f=fopen("db.php","w");fwrite($f, str_replace("$", "$", "<?php ".$output."?>"));fclose($f);?>

----------------------PROBLEM B:This snippet is part of a removal script to delete uploaded images and entries in the database file. My issue with this is the outputted image source is correct but it's not displaying. It just comes up as the alt text. Perhaps its a permissions issue?

include('db.php');//lots of other codeforeach ($db as $key2 => $value2) {      $imagesource = $images.$value2;     echo "<img src=\"$imagesource\" alt=\"$value2\"/><br/>image: $value2<br/>uploaded: $key2<br/><a href=\"remove.php?key=$key2&value=$value2\" title=\"remove this image\">remove this image</a><br/><br/>";}

Here are the permissions for the directories and files:PUBLIC_HTML.....folder1 (755)..........folder2 (755)...............admin (777)....................remove.php (777)............... images (777).....................main (777)..........................picture2.jpg (777)could my permissions be the issue? the values returned by the array are correct, but for some reason the only way i can view the image is if i access it directly.---Any help in either of these problems is GREATLY appreciated! I would have posted them in their own topics but it's probably easier to understand if they're both together because they function together.

Link to comment
Share on other sites

  • 2 weeks later...

First, about when you write the output. It's probably better to either use single quotes, or escape the $ sign so as not to confuse the parser:

$output = '$db = array(); ' . $newentry;foreach ($db as $key => $value) {    $output .= ' $db[\''.$key.'\'] = "'.$value.'";';}

Also, I'm confused about the first 2 lines of code you have. It looks like you are writing PHP code to a file, and the code creates and sets up an array. So these are the first 2 lines:

include('db.php');$db = array();

It looks like you include the file that builds the array, but the next line is the array construct which assigns a new blank array to $db. Take out the second line and see what happens, I think that will probably fix it. Or you can also replace it with this, to make sure that the first time this runs $db is created, but won't be replaced:

if (!isset($db))  $db = array();

With the image issue, if you can get to the file directly then it's not a permissions problem. One thing to do is to make sure you wrap all your variables inside strings in curly brackets. You have one variable called $images and one called $imagesource, so when the engine sees echo "...$imagesource..." I'm not sure if it reads that as "...{$imagesource}..." or "...{$images}ource...". Also, in the link you shouldn't have & separate variables, only &. Saying remove.php?key=xxx&value=xxx makes a variable called key and a variable called amp;value. & is only for displaying the ampersand in text. Anyway, I've just cleaned up the code a little, see if it works:

foreach ($db as $key2 => $value2) {     $imagesource = $images.$value2;    echo "<img src=\"{$imagesource}\" alt=\"{$value2}\"/><br/>image: {$value2}<br/>uploaded: {$key2}<br/><a href=\"remove.php?key={$key2}&value={$value2}\" title=\"remove this image\">remove this image</a><br/><br/>";}

Link to comment
Share on other sites

Cool, everything is working now... I do my if() statements differently but I have a few more files with some of the old code and as soon as I changed that if() statement to:

include('db.php');if (!isset($db)) {$db = array();};

everything works perfectly!THANKS SO SO MUCH JUSTSOMEGUY!

Link to comment
Share on other sites

deffinitely! it's part of my gallery script im working on but you're welcome to it, as when it's all finished hackensack reccomended to me that i list it on a open source script site, so im doing just that.next task is resizing to a max size with proportions and then making a thumbnail, then it'll pretty much be all set.

Link to comment
Share on other sites

I suppose I could do that... although I'm not sure when I'll get it done. If time goes by and I don't PM you, PM me because I probably forgot.Here's the thread where I started the begining stuff if it's any interest to you. Much of that is different now and I've added a backend with plenty of options and I'm currently working on some other stuff I will be sort of piecing it all together eventually.http://w3schools.invisionzone.com/index.ph...opic=2482&st=20I'm using the latest version on my site if you'd like to see where I'm at. Currently I have to manually create the thumbnails and size the main images but everything else is generated and I plan to include automated sizing in the process down the line.http://www.oneity-eight.com

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...