Jump to content

Ned help - File upload trying to show img uploaded in a different page


newbie2php

Recommended Posts

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.

Link to comment
Share on other sites

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 :P

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.