legohead6 Posted July 10, 2006 Share Posted July 10, 2006 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]<?PHPSESSION_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] Quote Link to comment https://forums.phpfreaks.com/topic/14195-solved-why-doesnt-this-code-work/ Share on other sites More sharing options...
obsidian Posted July 10, 2006 Share Posted July 10, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14195-solved-why-doesnt-this-code-work/#findComment-55639 Share on other sites More sharing options...
kenrbnsn Posted July 10, 2006 Share Posted July 10, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/14195-solved-why-doesnt-this-code-work/#findComment-55640 Share on other sites More sharing options...
kenrbnsn Posted July 10, 2006 Share Posted July 10, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/14195-solved-why-doesnt-this-code-work/#findComment-55646 Share on other sites More sharing options...
legohead6 Posted July 10, 2006 Author Share Posted July 10, 2006 I changed the array name after and it still didnt work! but obsidians code works perfectly!! here is the code know[code]<?PHPSESSION_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 Quote Link to comment https://forums.phpfreaks.com/topic/14195-solved-why-doesnt-this-code-work/#findComment-55649 Share on other sites More sharing options...
legohead6 Posted July 10, 2006 Author Share Posted July 10, 2006 also obsidian how do i make your code not case sensitive? Quote Link to comment https://forums.phpfreaks.com/topic/14195-solved-why-doesnt-this-code-work/#findComment-55653 Share on other sites More sharing options...
obsidian Posted July 10, 2006 Share Posted July 10, 2006 [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! Quote Link to comment https://forums.phpfreaks.com/topic/14195-solved-why-doesnt-this-code-work/#findComment-55656 Share on other sites More sharing options...
legohead6 Posted July 10, 2006 Author Share Posted July 10, 2006 Thanks that worked Quote Link to comment https://forums.phpfreaks.com/topic/14195-solved-why-doesnt-this-code-work/#findComment-55664 Share on other sites More sharing options...
legohead6 Posted July 10, 2006 Author Share Posted July 10, 2006 I got it! heres the code with editing! it deletes the file then recreates it![code]<?PHPSESSION_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] Quote Link to comment https://forums.phpfreaks.com/topic/14195-solved-why-doesnt-this-code-work/#findComment-55683 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.