newbie2php Posted September 23, 2008 Share Posted September 23, 2008 Hi I'm new this this site and php but I must say I am having a good time learning. I ran into a problem. I have a script to upload a file to the server. This works well <?php // Address error handling. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); if (isset($_POST['submit'])) { //Handle form. //Try to moce the uploaded file. if (move_uploaded_file ($_FILES['thefile']['tmp_name'], "../uploads/{$_FILES['thefile']['name']}")) { print '<p>Your file has been uploaded.</p>'; ?> <img src="../uploads/<?=$_FILES['thefile']['name']?>" /> <?php $_SESSION[filename]="../uploads/".$_FILES['thefile']['name']; } else {//Problem print '<p>Your file could not be uploaded because<b>'; //Print a message based upon the ettor. switch($_FILES['thefile']['error']) { case 1: print 'The file exceeds the upload_max_filesize setting in php.ini'; break; case 2: print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form'; break; case 3: print 'The file was only partially uploaded'; break; case 4: print 'No file was uploaded'; break; } print '</b>.</p>'; } } //End of the SUBMIT IF. Leace php and display form ?> <form action ="upload_file2.php" enctype="multipart/form-data" method="post"> <p>Upload a file using this form:<br /><br /> <input type ="hidden" name="MAX_FILE_SIZE" value="30000" /> <input type = "file" name="thefile" /><br /><br /> <input type="submit" name ="submit" value ="Upload This File" /> </form> Then I have code on another page To show the file <img src="<?=$_SESSION[filename]?>"> But for somereason I can not get it to work. Can some one help me understand what i did wrong and help me? PLEASE and thank you. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted September 23, 2008 Share Posted September 23, 2008 The first thing i notice is that you have not called session_start() at the top of the page. You must use this function in your script prior to output and prior to using any session variables(so, the safest and best place is as the first line of your script) and it must be used in all pages that use those session variables. Also, you should really try to indent your code properly. You'll make your life so much easier. Oh, and try to use the tags when you post your code on the forums. Since you're new, I wont berate you for the lack of them in your last post Quote Link to comment Share on other sites More sharing options...
newbie2php Posted September 23, 2008 Author Share Posted September 23, 2008 wow ok so I will work on indenting the code. I still have a lot to learn so thank you for being kind about it. I did add the code as you suggested and guess what. I got an error Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/nikki/public_html/EducationalBooks/upload_file2.php:10) in /home/nikki/public_html/EducationalBooks/upload_file2.php on line 12 Gotta love php Anyway if you can help that would be great. Thank you Quote Link to comment Share on other sites More sharing options...
newbie2php Posted September 23, 2008 Author Share Posted September 23, 2008 I figured out my problem. I had html code before my php. Thank you so very much. Now that I am able to get it to work I see I need to write and if esle statement for displaying the image right now the code <code><img src="<?=$_SESSION[filename]?>" > </code> works great but it shows a broken image until the user uploads an image. Can anyone tell me of a tutorial that might help me out with this? Any help is great Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted September 23, 2008 Share Posted September 23, 2008 Not sure there'll be a tutorial about this. Your best bet is google what you're trying to do, which, in this case, is work out if a file exists. In fact, the function is just that: file_exists() Quote Link to comment 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.