The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - [PHP FORM] File Upload
Discuss [PHP FORM] File Upload in the PHP Development forum on Dev Shed. [PHP FORM] File Upload PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 8th, 2013, 07:47 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 69

Time spent in forums: 14 h 5 m 2 sec
Reputation Power: 1
|
|
|
PHP-General - [PHP FORM] File Upload
I am having problems uploading a picture to my site.
My HTML Form:
Code:
<form action="inc/doAddServer.inc.php" enctype="multipart/form-data" method="post">
<div class="control-group">
<label class="control-label" for="serverImg">Server Image Banner</label>
<div class="controls">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input name="serverImg" type="file">
</div>
</div>
</form>
And if I try to use the other fields (I only included the one for the upload above) I use a is not set function for the serverImg, because then I don't want to use the upload. The problem I am having even though the upload isn't set, it still uses the code that when there is a file there it uses.
I find it hard to explain.
PHP Code:
if(!isset($serverImg)){
$sql=mysql_query("INSERT INTO servers (serverName, serverIP, serverOwner, serverWebsite, serverDesc, serverUptime, serverPlayersOnline, serverPlayersTotal, serverDateAdded) VALUES ('$serverName', '$serverIP', '$serverOwner', '$serverWebsite', '$serverDesc', '$serverUptime', '$serverPlayersOnline', '$serverPlayersTotal', '$today')");
header('Location: ../addServer.php?s='.urlencode('Thank you for adding your server to our database!')); exit;
} else {
// Your file name you are uploading
$file_name = $HTTP_POST_FILES['serverImg']['name'];
// random 4 digit to add to our file name
// some people use date and time in stead of random digit
$random_digit=$nextId;
//combine random digit to you file name to create new file name
//use dot (.) to combile these two variables
$new_file_name=$random_digit;
//set where you want to store files
//in this example we keep file in folder upload
//$new_file_name = new upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "../banners/".$new_file_name;
if($serverImg != none) {
if(copy($HTTP_POST_FILES['serverImg']['tmp_name'], $path)) {
$sql=mysql_query("INSERT INTO servers (serverName, serverIP, serverOwner, serverWebsite, serverDesc, serverUptime, serverPlayersOnline, serverPlayersTotal, serverDateAdded) VALUES ('$serverName', '$serverIP', '$serverOwner','$serverWebsite', '$serverDesc', '$serverUptime', '$serverPlayersOnline', '$serverPlayersTotal', '$today')");
header('Location: ../addServer.php?s='.urlencode('Thank you for adding your server to our database!')); exit;
} else {
echo "Error";
}
} else {
$sql=mysql_query("INSERT INTO servers (serverName, serverIP, serverOwner, serverWebsite, serverDesc, serverUptime, serverPlayersOnline, serverPlayersTotal, serverDateAdded) VALUES ('$serverName', '$serverIP', '$serverOwner', '$serverWebsite', '$serverDesc', '$serverUptime', '$serverPlayersOnline', '$serverPlayersTotal', '$today')");
header('Location: ../addServer.php?s='.urlencode('Thank you for adding your server to our database!')); exit;
}
}
Any help would be greatly appreciated!
Thanks,
TJ
|

March 8th, 2013, 08:09 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
1. It will always be set.
2. Unless you have an assignment for $serverImg somewhere you haven't shown, the variable does not exist and you need to use the $_FILES array...
3. $HTTP_POST_FILES died a long time ago. It's now called $_FILES. Same structure, different name.
4. You aren't using a "is not set function". You are trying to compare it to the constant "none" which doesn't exist.
To check what happened with the file upload look at the ['error']. It will be one of these constants. UPLOAD_ERR_OK means the image was uploaded file and anything else means the upload didn't happen correctly. Specifically, UPLOAD_ERR_NO_FILE means there wasn't a file to begin with so you might not want to treat that like an actual error.
|

March 8th, 2013, 08:36 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 28
  
Time spent in forums: 1 Day 1 h 38 m 49 sec
Reputation Power: 0
|
|
Here's some snippets of code from my upload picture section, maybe it will help?
PHP Code:
if (isset($_POST['submit'])) {
$allowedExts = array("jpg", "jpeg", "gif", "png");
$basenameAndExtension = explode('.', $_FILES["photo"]["name"]);
$extension = end($basenameAndExtension);
if ((($_FILES["photo"]["type"] == "image/gif")
|| ($_FILES["photo"]["type"] == "image/jpeg")
|| ($_FILES["photo"]["type"] == "image/png")
|| ($_FILES["photo"]["type"] == "image/pjpeg"))
&& ($_FILES["photo"]["size"] < 800 * 1024)
&& in_array($extension, $allowedExts))
{
if ($_FILES["photo"]["error"] > 0)
PHP Code:
if (file_exists("upload/" . $_FILES["photo"]["name"]))
{
echo $_FILES["photo"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["photo"]["tmp_name"],
"upload/" . $_FILES["photo"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["photo"]["name"];
$image_name = $_FILES["photo"]["name"];
echo "<br />";
echo "<img src=\"http://localhost/php_test/greencay_php/upload/" . $image_name . "\" alt=\"There ya go\" />";
echo "<br />";
}
Just wanted to add I didn't sanitize my code yet 
|

March 8th, 2013, 09:14 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 69

Time spent in forums: 14 h 5 m 2 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by requinix 1. It will always be set.
2. Unless you have an assignment for $serverImg somewhere you haven't shown, the variable does not exist and you need to use the $_FILES array...
3. $HTTP_POST_FILES died a long time ago. It's now called $_FILES. Same structure, different name.
4. You aren't using a "is not set function". You are trying to compare it to the constant "none" which doesn't exist.
To check what happened with the file upload look at the ['error']. It will be one of these constants. UPLOAD_ERR_OK means the image was uploaded file and anything else means the upload didn't happen correctly. Specifically, UPLOAD_ERR_NO_FILE means there wasn't a file to begin with so you might not want to treat that like an actual error. |
Okay so, how would I use the UPLOAD_ERR_NO_FILE here?
PHP Code:
if($serverImg != none) {
if(copy($HTTP_POST_FILES['serverImg']['tmp_name'], $path)) {
$sql=mysql_query("INSERT INTO servers (serverName, serverIP, serverOwner, serverWebsite, serverDesc, serverUptime, serverPlayersOnline, serverPlayersTotal, serverDateAdded) VALUES ('$serverName', '$serverIP', '$serverOwner','$serverWebsite', '$serverDesc', '$serverUptime', '$serverPlayersOnline', '$serverPlayersTotal', '$today')");
header('Location: ../addServer.php?s='.urlencode('Thank you for adding your server to our database!')); exit;
} else {
echo "Error";
}
} else {
$sql=mysql_query("INSERT INTO servers (serverName, serverIP, serverOwner, serverWebsite, serverDesc, serverUptime, serverPlayersOnline, serverPlayersTotal, serverDateAdded) VALUES ('$serverName', '$serverIP', '$serverOwner', '$serverWebsite', '$serverDesc', '$serverUptime', '$serverPlayersOnline', '$serverPlayersTotal', '$today')");
header('Location: ../addServer.php?s='.urlencode('Thank you for adding your server to our database!')); exit;
}
Would it be this first line?
So like this?
PHP Code:
if(UPLOAD_ERR_NO_FILE !== 4){ // Because -UPLOAD_ERR_NO_FILE - Value: 4; No file was uploaded.
Thanks.
|

March 8th, 2013, 09:15 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 69

Time spent in forums: 14 h 5 m 2 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by Strider64 Here's some snippets of code from my upload picture section, maybe it will help?
PHP Code:
if (isset($_POST['submit'])) {
$allowedExts = array("jpg", "jpeg", "gif", "png");
$basenameAndExtension = explode('.', $_FILES["photo"]["name"]);
$extension = end($basenameAndExtension);
if ((($_FILES["photo"]["type"] == "image/gif")
|| ($_FILES["photo"]["type"] == "image/jpeg")
|| ($_FILES["photo"]["type"] == "image/png")
|| ($_FILES["photo"]["type"] == "image/pjpeg"))
&& ($_FILES["photo"]["size"] < 800 * 1024)
&& in_array($extension, $allowedExts))
{
if ($_FILES["photo"]["error"] > 0)
PHP Code:
if (file_exists("upload/" . $_FILES["photo"]["name"]))
{
echo $_FILES["photo"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["photo"]["tmp_name"],
"upload/" . $_FILES["photo"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["photo"]["name"];
$image_name = $_FILES["photo"]["name"];
echo "<br />";
echo "<img src=\"http://localhost/php_test/greencay_php/upload/" . $image_name . "\" alt=\"There ya go\" />";
echo "<br />";
}
Just wanted to add I didn't sanitize my code yet  |
Thanks, I will look at this after I learn mine... I'm trying to learn PHP. But if I can't get it, I will use this! 
|

March 9th, 2013, 08:25 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 69

Time spent in forums: 14 h 5 m 2 sec
Reputation Power: 1
|
|
@ Strider64
Thanks. I ended up using your code! It works like a charm. Thanks 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|