AmericanAlien
-
Posts
18 -
Joined
-
Last visited
Never
Posts posted by AmericanAlien
-
-
FINAL WORKING CODE EXAMPLE OF UPLOADING IMAGES AND DISPLAYING THEM BY PUTTING SOME REQUIRED RESTRICTIONS ON THE UPLOADED FILE:
NOTE: BEFORE USING MY CODE CREATE ThE FOLDER named as 'upload' from where you are running my code.
Upload form.php
<html> <body> <form action="upload-restrict.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html>
upload-restrict.php
<?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg")) && ($_FILES["file"]["size"] < 2000000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; // echo '<img src = . "upload/" . $_FILES["file"]["name"] />'; echo "<br />"; echo "<br />"; echo "<img src='upload/" . $_FILES["file"]["name"]. "'>" ; // File is getting displayed from here } } } else { if (($_FILES["file"]["type"] != "image/jpeg")) echo "Invalid file TYPE"; if (($_FILES["file"]["size"] < 20000)) echo "\nInvalid file size"; else { echo "Invalid file unknwon reason"; } } ?>
The answer of your question if you mean the sources "PATH of the image like upload/picname.jpeg" All you need to do is assign that path to $pic and <img src = "<?php echo $pic; ?>" width="100" height="100" /> will display the image on the page.
NOTE: BEFORE USING MY CODE CREATE ThE FOLDER named as 'upload' from where you are running my code.
The code is working AWESOMELY! The actually shows up this time. I appreciate your hard work. One thing. This is a permanent page, the pics do not stay in that page so others can view it. Is this not possible? What I mean is, is there a way to have each picture that is upload to stay on the webpage in the order uploaded and others can view this page as well?
-
Following code is the path of image into the variable and simply displaying the image from that path. If you know the path where the image is being saved temporarily on your server then you can easily display it by using the following code but make sure your image is getting saved first..
<img src = "<?php echo $pic; ?>" width="100" height="100" />
Thanks for the help! I had to go in another direction for now. A quick question, how is that code displaying the pic in a new page? I see it sources the pic, but I do not see how it takes the pic and places it into an html or even php page in a type of gallery.
-
The header() function cannot be used after output has been sent to browser. This includes any HTML outside of PHP, and whitespace outside of PHP, or any echo, print, print_r, etc. (any function or construct that outputs to the page, including errors, notices, etc.).
Ok, from what I am reading, I do need the header, unless I am interpreting it incorrectly. How would I achieve this type of result?
-
The header() function cannot be used after output has been sent to browser. This includes any HTML outside of PHP, and whitespace outside of PHP, or any echo, print, print_r, etc. (any function or construct that outputs to the page, including errors, notices, etc.).
header("Location:display.php?imageName=".$file_name ); exit();
So this line is not needed? However, why is the display.php not showing up? I am not following that. So, if I get rid of that header, what route do I use to get location?
-
YOUR FULL CODE ( tested and it working)
Main Page
<?php /* "Warning: Cannot modify header information - headers already sent by " To avoid the header error , give value zero to $mosConfig_locale_debug = 0; $mosConfig_locale_use_gettext = 0; */ $mosConfig_locale_debug = 0; $mosConfig_locale_use_gettext = 0; ob_start(); /*This script can send an email and/or make an entry in a log file There are two variables below - one for an email address and one for a log file Set both variables to the values you want to use If you do not want either an email or log entry, comment out the respective line For example, if you do not want an email sent, put a // in front of the $emailAddress line - same for the $logFile line */ $logFile = $_SERVER['DOCUMENT_ROOT'].'/upload.log';// full path to your log $emailaddress = "email@gmail.com"; $home_page = "index.html"; // used for a link to return $uploaddir = "uploads/"; // the directory where files are to be uploaded - include the trailing slash $fileTypeArray = array(".jpg",".gif",".txt"); // enter in all lower case, the script will handle a match with upper case $maxSize = 500000; // maximum file size that can be uploaded - in bytes $maxFileSpace = 50000000; // maximum space that can be used by files matching the $fileTypeArray array in the upload directory - in bytes putenv('TZ=EST5EDT'); // eastern time// change nothing below this line $maxDisplay = $maxSize / 1000; ?> <html><head></head><body><div style="text-align: center; margin: 100px auto; border: 1px black solid; width:400px;"> <?php $file_name = $_FILES['file']['name']; $file_size = $_FILES['file']['size']; $file_tmp_name = $_FILES['file']['tmp_name']; if (!empty($file_name)) { unset($error); echo "<br>File Name: $file_name<br><br>"; echo "File Size: $file_size bytes<br><br>"; // file size test if ($file_size == 0 ) $error .= "<span style='color: red;'>Invalid file</span><br>"; if ($file_size > $maxSize ) $error .= "<span style='color: red;'>Your file exceeds $maxDisplay K.</span><br>"; // file type test if (!in_array(strtolower(strrchr($file_name,'.')),$fileTypeArray) ) $error .= "<span style='color: red;'>Your file is not a valid file type.</span><br>"; // max directory size test foreach(scandir($uploaddir) as $file_select) if (in_array(strtolower(strstr($file_select,'.')),$fileTypeArray)) $total_size = $total_size + filesize($uploaddir.$file_select); if (($total_size + $file_size) >= $maxFileSpace) $error .= "<span style='color: red;'>Total file space limits have been exceeded.</span><br>"; // scrub characters in the file name $file_name = stripslashes($file_name); $file_name = preg_replace("#[ ]#","_",$file_name); // change spaces to underscore $file_name = preg_replace('#[^()\.\-,\w]#','_',$file_name); //only parenthesis, underscore, letters, numbers, comma, hyphen, period - others to underscore $file_name = preg_replace('#(_)+#','_',$file_name); //eliminate duplicate underscore // check for file already exists if (file_exists($uploaddir.$file_name)) $error .= "<span style='color: red;'>File already exists.</span><br>"; // if all is valid, do the upload if (empty($error)) { if (move_uploaded_file($file_tmp_name,$uploaddir.$file_name)) { chmod($uploaddir.$file_name,0644); //echo "<span style='color: green;'>Your file was successfully uploaded!</span>"; /* 1st send the email confirmation to user 2nd redirect to display.php to show the image */ if (isset($emailAddress)) { $message = $file_name . " was uploaded by".$_SERVER['REMOTE_ADDR']."at".date('Y-m-d H:i:s'); mail($emailaddress,"You have a file upload",$message,"From: Website <>"); } if (isset($logFile)) { $logData = $file_name."||".$_SERVER['REMOTE_ADDR']."||".date('Y-m-d H:i:s')."\r\n"; @file_put_contents($logFile,$logData,FILE_APPEND|LOCK_EX); } ## redirect to display.php header("Location:display.php?imageName=".$file_name ); exit(); } else { echo "<span style='color: red;'>Your file could not be uploaded.</span>"; } } echo "$error<hr>";} ?> <p>Upload a <span style="color: blue;"> <?php foreach($fileTypeArray as $fileType) echo $fileType;?> </span> file to our server<br>Maximum file size is <?php echo $maxDisplay; ?>K</p> <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" enctype="multipart/form-data"> File: <input type="file" name="file" style="width: 250px;"><br> <input type="submit" name="submit" value="Upload File"> </form><a href="<?php echo $home_page; ?>">Return to the Home Page</a>
display.php
<?php // please validate the input $uploaddir = "uploads/"; // the directory where files are to be uploaded - include the trailing slash $file_name = $_GET['imageName']; echo "<img src='$uploaddir/$file_name' />"; ?>
I tried the code again and it still errors. Are you getting a page with the image (from display.php?) because I do not and I just get the confirmation page, and when I go to the display.php, it shows me nothing but a broken image.
Attached is the error (error 77 is still the same line of code.)
header("Location:display.php?imageName=".$file_name ); exit();
I have used your code exactly and just used the basics.
[attachment deleted by admin]
-
WEll i think it should be possible
I give you two suggestions:
1: Use the database for the form data.
2: If you don't want to use database then you have to store image temporarily some where on your server in order to display image onto the next page by simply giving its path to the following code.
You can assign the image if its not big enough to a variable as well and by using header function displaying the image (If i am not wrong)
<img src = "<?php echo $row_Recordset1['pic']; ?>" width="100" height="100" />
I CAN HELP YOU WITH BLOB....
That is interesting, I didn't think about echo $row, This would need the database built, no? Or is this doing its own row built from the file on the server? That might be the way to go. I will try to see if I can do this. BLOB is definitely a weak point for me, this looks like very good practice for me.
-
Well i didn't check your code but what i did is after storing the form data to my database i get the id of my most recent used id where all of the input data is saved including the image's path
I used the following code to retrieve the image:
<img src="get.php?id=<?php echo $row_Recordset1['ID']; ?>" width="270" height="270" /> // FOR BLOB FIELD
<img src = "<?php echo $row_Recordset1['pic']; ?>" width="100" height="100" /> // FOR CERTAIN DIRECTORY WHOSE PATH IS SAVED IN THE DATABASE
The database option might be the only way to go. I was really hoping I could just pull the images from my upload file (after the upload) and then have them displayed in a new page via php.
-
After <? php
I figured, I just wanted to be safe, but I still get the same error, and I still do not have any images on the display.php page. I am not sure what is going on. I have tried to get this down.
-
place this code top of codes.
/* "Warning: Cannot modify header information - headers already sent by " To avoid the header error , give value zero to $mosConfig_locale_debug = 0; $mosConfig_locale_use_gettext = 0; */ $mosConfig_locale_debug = 0; $mosConfig_locale_use_gettext = 0; ob_start();
Place this on top of which portion of the code? The main code? I will try that. Thanks for help!
-
header("Location:".$config_basedir ."display.php?imageName=".$file_name ); exit();
where is it?
I am not understanding your question. Below is the code you suggested. You said to add the code (error 183) below the "successfully uploaded!" portion.
After this code.
echo "<span style='color: green;'>Your file was successfully uploaded!</span>";
Add new code
echo "<span style='color: green;'>Your file was successfully uploaded!</span>";echo "<img src='$uploaddir.$file_name' />"; $config_basedir="http://www.XXX.com/"; header("Location:".$config_basedir ."display.php?imageName=".$file_name ); exit();
display.php
<?php // please validate the input $uploaddir = "uploads/"; // the directory where files are to be uploaded - include the trailing slash $file_name = $_GET['imageName']; echo "<img src='$uploaddir.$file_name' />"; ?>
If this isn't what you mean, I really do not understand what you mean.
-
in the main page where is line 183?
As from my previous post line 183 is:
header("Location:".$config_basedir ."display.php?imageName=".$file_name ); exit();
-
from where would you like to display the uploaded image blob field or from any certain directory??
I want the images displayed in another webpage and for it to go directly to that page after the upload. The file it pulls the image from is uploads. As you can see from the code, it is trying to pull from that folder.
-
My code is as follows:
<?php /* This script can send an email and/or make an entry in a log file There are two variables below - one for an email address and one for a log file Set both variables to the values you want to use If you do not want either an email or log entry, comment out the respective line For example, if you do not want an email sent, put a // in front of the $emailAddress line - same for the $logFile line */ $logFile = $_SERVER['DOCUMENT_ROOT'].'/upload.log'; // full path to your log file $emailaddress = "email@gmail.com"; $home_page = "index.html"; // used for a link to return $uploaddir = "uploads/"; // the directory where files are to be uploaded - include the trailing slash $fileTypeArray = array(".jpg",".gif",".txt"); // enter in all lower case, the script will handle a match with upper case $maxSize = 500000; // maximum file size that can be uploaded - in bytes $maxFileSpace = 50000000; // maximum space that can be used by files matching the $fileTypeArray array in the upload directory - in bytes putenv('TZ=EST5EDT'); // eastern time // change nothing below this line $maxDisplay = $maxSize / 1000; ?> <html><head></head><body> <div style="text-align: center; margin: 100px auto; border: 1px black solid; width:400px;"> <?php // print_r($_FILES); // can be used for debugging $file_name = $_FILES['file']['name']; $file_size = $_FILES['file']['size']; $file_tmp_name = $_FILES['file']['tmp_name']; if (!empty($file_name)) { unset($error); echo "<br>File Name: $file_name<br><br>"; echo "File Size: $file_size bytes<br><br>"; // file size test if ($file_size == 0 ) $error .= "<span style='color: red;'>Invalid file</span><br>"; if ($file_size > $maxSize ) $error .= "<span style='color: red;'>Your file exceeds $maxDisplay K.</span><br>"; // file type test if (!in_array(strtolower(strrchr($file_name,'.')),$fileTypeArray) ) $error .= "<span style='color: red;'>Your file is not a valid file type.</span><br>"; // max directory size test foreach(scandir($uploaddir) as $file_select) if (in_array(strtolower(strstr($file_select,'.')),$fileTypeArray)) $total_size = $total_size + filesize($uploaddir.$file_select); if (($total_size + $file_size) >= $maxFileSpace) $error .= "<span style='color: red;'>Total file space limits have been exceeded.</span><br>"; // scrub characters in the file name $file_name = stripslashes($file_name); $file_name = preg_replace("#[ ]#","_",$file_name); // change spaces to underscore $file_name = preg_replace('#[^()\.\-,\w]#','_',$file_name); //only parenthesis, underscore, letters, numbers, comma, hyphen, period - others to underscore $file_name = preg_replace('#(_)+#','_',$file_name); //eliminate duplicate underscore // check for file already exists if (file_exists($uploaddir.$file_name)) $error .= "<span style='color: red;'>File already exists.</span><br>"; // if all is valid, do the upload if (empty($error)) { if (move_uploaded_file($file_tmp_name,$uploaddir.$file_name)) { chmod($uploaddir.$file_name,0644); echo "<span style='color: green;'>Your file was successfully uploaded!</span>"; if (isset($emailAddress)) { $message = $file_name . " was uploaded by".$_SERVER['REMOTE_ADDR']."at".date('Y-m-d H:i:s'); mail($emailaddress,"You have a file upload",$message,"From: Website <>"); } if (isset($logFile)) { $logData = $file_name."||".$_SERVER['REMOTE_ADDR']."||".date('Y-m-d H:i:s')."\r\n"; @file_put_contents($logFile,$logData,FILE_APPEND|LOCK_EX); } } else { echo "<span style='color: red;'>Your file could not be uploaded.</span>"; } } echo "$error<hr>"; } ?> <p>Upload a <span style="color: blue;"> <?php foreach($fileTypeArray as $fileType) echo $fileType; ?> </span> file to our server<br> Maximum file size is <?php echo $maxDisplay; ?>K</p> <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" enctype="multipart/form-data"> File: <input type="file" name="file" style="width: 250px;"><br> <input type="submit" name="submit" value="Upload File"></form> <a href="<?php echo $home_page; ?>">Return to the Home Page</a>
and the display.php is:
<?php // please validate the input $uploaddir = "uploads/"; // the directory where files are to be uploaded - include the trailing slash $file_name = $_GET['imageName']; echo "<img src='$uploaddir.$file_name' />"; ?>
I get this error: Warning: Cannot modify header information - headers already sent by (output started at /homepages/1/d225596241/htdocs/enter.php:6) in /homepages/1/d225596241/htdocs/enter.php on line 183
Line 183 is:
header("Location:".$config_basedir ."display.php?imageName=".$file_name ); exit();
The pic shows up after the upload, but it does not go to the display.php page nor does it automatically go to that page. Also, after the upload and the pic shows up the error I provided is show below.
Any ideas?
-
You have been a tremendous help. I just cannot get it to work, I must be missing something, or a path or a pattern. I will keep trying. I know it is late, but hopefully I can get it to work soon.
-
Yes, that is what I am trying to achieve. I am just not succeeding at it.
-
Well, I have to admit, that is the closest I have gotten. The pic does show up. THANK YOU! However, I would like it to show up to a new webpage and stay there. Something like: <form method="post" action="gallery.php">
Would something like this work? Have the php shoot to that upload gallery and then have the gallery display with the new image?
-
Hello all,
I am having an issue. I have read many articles and help forums, but I cannot find a specific way to help me. My issue is this. I have a php upload form for visitors to upload an image. Now, I would like to know how to get the uploaded images to show up after the upload. Someone suggested to me to call the images. However, I am not sure how to do this. Below is the script for the image upload, which works great for uploading.
<?php
/*
This script can send an email and/or make an entry in a log file
There are two variables below - one for an email address and one for a log file
Set both variables to the values you want to use
If you do not want either an email or log entry, comment out the respective line
For example, if you do not want an email sent, put a // in front of the $emailAddress line - same for the $logFile line
*/
$logFile = $_SERVER['DOCUMENT_ROOT'].'/upload.log'; // full path to your log file
$emailaddress = "email@gmail.com";
$home_page = "index.html"; // used for a link to return
$uploaddir = "uploads/"; // the directory where files are to be uploaded - include the trailing slash
$fileTypeArray = array(".jpg",".gif",".txt"); // enter in all lower case, the script will handle a match with upper case
$maxSize = 500000; // maximum file size that can be uploaded - in bytes
$maxFileSpace = 50000000; // maximum space that can be used by files matching the $fileTypeArray array in the upload directory - in bytes
putenv('TZ=EST5EDT'); // eastern time
// change nothing below this line
$maxDisplay = $maxSize / 1000;
?>
<html><head></head><body>
<div style="text-align: center; margin: 100px auto; border: 1px black solid; width:400px;">
<?php
// print_r($_FILES); // can be used for debugging
$file_name = $_FILES['file']['name'];
$file_size = $_FILES['file']['size'];
$file_tmp_name = $_FILES['file']['tmp_name'];
if (!empty($file_name)) {
unset($error);
echo "<br>File Name: $file_name<br><br>";
echo "File Size: $file_size bytes<br><br>";
// file size test
if ($file_size == 0 ) $error .= "<span style='color: red;'>Invalid file</span><br>";
if ($file_size > $maxSize ) $error .= "<span style='color: red;'>Your file exceeds $maxDisplay K.</span><br>";
// file type test
if (!in_array(strtolower(strrchr($file_name,'.')),$fileTypeArray) ) $error .= "<span style='color: red;'>Your file is not a valid file type.</span><br>";
// max directory size test
foreach(scandir($uploaddir) as $file_select) if (in_array(strtolower(strstr($file_select,'.')),$fileTypeArray)) $total_size = $total_size + filesize($uploaddir.$file_select);
if (($total_size + $file_size) >= $maxFileSpace) $error .= "<span style='color: red;'>Total file space limits have been exceeded.</span><br>";
// scrub characters in the file name
$file_name = stripslashes($file_name);
$file_name = preg_replace("#[ ]#","_",$file_name); // change spaces to underscore
$file_name = preg_replace('#[^()\.\-,\w]#','_',$file_name); //only parenthesis, underscore, letters, numbers, comma, hyphen, period - others to underscore
$file_name = preg_replace('#(_)+#','_',$file_name); //eliminate duplicate underscore
// check for file already exists
if (file_exists($uploaddir.$file_name)) $error .= "<span style='color: red;'>File already exists.</span><br>";
// if all is valid, do the upload
if (empty($error)) {
if (move_uploaded_file($file_tmp_name,$uploaddir.$file_name)) {
chmod($uploaddir.$file_name,0644);
echo "<span style='color: green;'>Your file was successfully uploaded!</span>";
if (isset($emailAddress)) {
$message = $file_name . " was uploaded by".$_SERVER['REMOTE_ADDR']."at".date('Y-m-d H:i:s');
mail($emailaddress,"You have a file upload",$message,"From: Website <>");
}
if (isset($logFile)) {
$logData = $file_name."||".$_SERVER['REMOTE_ADDR']."||".date('Y-m-d H:i:s')."\r\n";
@file_put_contents($logFile,$logData,FILE_APPEND|LOCK_EX);
}
} else {
echo "<span style='color: red;'>Your file could not be uploaded.</span>";
}
}
echo "$error<hr>";
}
?>
<p>Upload a <span style="color: blue;">
<?php
foreach($fileTypeArray as $fileType) echo $fileType;
?>
</span> file to our server<br>
Maximum file size is <?php echo $maxDisplay; ?>K</p>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" enctype="multipart/form-data">
File: <input type="file" name="file" style="width: 250px;"><br>
<input type="submit" name="submit" value="Upload File"></form>
<a href="<?php echo $home_page; ?>">Return to the Home Page</a>
</div>
What is the method or process to get the images to show up or to pull the images from the folder to a webpage?
Display images after image Upload
in PHP Coding Help
Posted
Trust me, you have helped me a ton. I understand the loop route you speak of. I have tried that from the start, but maybe my paths are not inputted correctly. Maybe I can do what I do with my forms and have the info (pics)inputted into a form type php document. I think that may work, I have not tried that yet. Your code was the first code that actually worked, and I think I can use that and use my form method and integrate them or mesh them together. At least I hope so.
Again, thanks for your tremendous help, absolutely appreciated!