Jump to content


Photo

*SOLVED* why doesnt this code work??


  • Please log in to reply
8 replies to this topic

#1 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 10 July 2006 - 04:45 PM

THis code is supposed to find if the file is a picture or not and if it is display the pic(witch it does) and if not display the error! but right now if the pics good it shows it if not it shows a box with x! not the error!

<?PHP
SESSION_START();
$file1=$_GET['f'];
$file = base64_decode("$file1");
$folder=$_SESSION['folder'];
$picture = array('gif', 'jpeg', 'jpg', 'pjpeg');
if (eregi("$pictures", $file)){
$pic="$folder/$file";
echo "<img src=$pic>";
}else{
echo "This is not a picture";
}
?>

"Syntax error" .. WHERE THE **** IS IT?!

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 10 July 2006 - 04:51 PM

for one thing, you're referencing a variable $pictures when the only one you've instantiated is $picture. also, you're using the array wrong in your eregi() call. if you're simply wanting to check the final extension on the filename and compare it to the array, why don't you try doing something like this:
<?php
$types = array('gif', 'jpeg', 'jpg', 'pjpeg');
$parts = explode('.', $filename);
$key = count($parts) - 1;
$ext = $parts[$key];
if (in_array($ext, $types)) {
  // valid file extension
} else {
  // invalid file extension
}
?>

you may want to actually do your comparison on the MIME type of the uploaded file, though, so you're not depending on the user to actually name their file correctly.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 July 2006 - 04:51 PM

Your problem might lie in these two lines:
<?php
$picture = array('gif', 'jpeg', 'jpg', 'pjpeg');
if (eregi("$pictures", $file)){
?>
You define the variable $picture, but seem to be using the variable $pictures in your comparison.
Also, you don't have to put the quotes around the variable name.

Ken

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 July 2006 - 05:00 PM

Another way to get the file extension is to use the pathinfo() function:
<?php
$fn = '/this/is/a/test.of.a.file.with.multiple.dots.php';
$x = pathinfo($fn);
echo 'The file extension for ' . $fn . ' is [' . $x['extension'] . ']';
?>

Ken

#5 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 10 July 2006 - 05:04 PM

I changed the array name after and it still didnt work! but obsidians code works perfectly!! here is the code know

<?PHP
SESSION_START();
$file1=$_GET['f'];
$file = base64_decode("$file1");
$folder=$_SESSION['folder'];
$types = array('gif', 'jpeg', 'jpg', 'pjpeg');
$parts = explode('.', $file);
$key = count($parts) - 1;
$ext = $parts[$key];
if (in_array($ext, $types)) {
$path="$folder/$file";
echo "<img src=$path>";
} else {
$path="$folder/$file";
$text = file_get_contents($path);
echo "$text";
}
echo "<br><a href=manager.php>Go Back</a>";
?>

now how would i make it so i can edit the text and save the file again? fwrite() makes it all weird i need like fupdate() lol..... Please help

"Syntax error" .. WHERE THE **** IS IT?!

#6 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 10 July 2006 - 05:12 PM

also obsidian how do i make your code not case sensitive?
"Syntax error" .. WHERE THE **** IS IT?!

#7 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 10 July 2006 - 05:14 PM

also obsidian how do i make your code not case sensitive?


just run a strtolower() on your $ext. that way, you're ALWAYS comparing lower case to lower case:
<?php
$ext = strtolower($parts[$key]);
if (in_array($ext, $types)) {
  $path="$folder/$file";
  echo "<img src=$path>";
} else {
  $path="$folder/$file";
  $text = file_get_contents($path);
  echo "$text";
}
?>

good luck!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#8 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 10 July 2006 - 05:24 PM

Thanks that worked
"Syntax error" .. WHERE THE **** IS IT?!

#9 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 10 July 2006 - 05:51 PM

I got it! heres the code with editing! it deletes the file  then recreates it!

<?PHP
SESSION_START();
$file1=$_GET['f'];
$file = base64_decode("$file1");
$folder=$_SESSION['folder'];
$types = array('gif', 'jpeg', 'jpg', 'pjpeg');
$parts = explode('.', $file);
$key = count($parts) - 1;
$ext = strtolower($parts[$key]);
if (in_array($ext, $types)) {
$path="$folder/$file";
$path="$folder/$file";
$path2 = str_replace("_"," ", $path);
echo "<img src=$path2>";
} else {
$path="$folder/$file";
$path2 = str_replace("_"," ", $path);
$text = file_get_contents($path2);
echo "<form method=POST><textarea name=content rows=50 cols=100>$text</textarea><br><input type=submit name=submit value=update>";
if(isset($_POST['submit'])){
unlink("$folder/$file");
$page = fopen("$folder/$file", 'x+');
$content=$_POST['content'];
fwrite($page, $content);
echo "Updated Successfully";
}
}
echo "<br><a href=manager.php>Go Back</a>";
?>

"Syntax error" .. WHERE THE **** IS IT?!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users