Jump to content

*SOLVED* why doesnt this code work??


legohead6

Recommended Posts

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!

[code]<?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";
}
?>[/code]
Link to comment
Share on other sites

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:
[code]
<?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
}
?>
[/code]

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.
Link to comment
Share on other sites

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

Ken
Link to comment
Share on other sites

Another way to get the file extension is to use the pathinfo() function:
[code]<?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'] . ']';
?>[/code]

Ken
Link to comment
Share on other sites

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

[code]<?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>";
?>[/code]

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
Link to comment
Share on other sites

[quote author=legohead6 link=topic=100075.msg394540#msg394540 date=1152551525]
also obsidian how do i make your code not case sensitive?
[/quote]

just run a strtolower() on your $ext. that way, you're ALWAYS comparing lower case to lower case:
[code]
<?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";
}
?>
[/code]

good luck!
Link to comment
Share on other sites

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

[code]<?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>";
?>[/code]
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.