PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 8th, 2013, 07:47 PM
tjswebdev tjswebdev is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 69 tjswebdev User rank is Private First Class (20 - 50 Reputation Level)tjswebdev User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 14 h 5 m 2 sec
Reputation Power: 1
Send a message via Skype to tjswebdev
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

Reply With Quote
  #2  
Old March 8th, 2013, 08:09 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,703 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 5 h 39 m 40 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to 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.

Reply With Quote
  #3  
Old March 8th, 2013, 08:36 PM
Strider64 Strider64 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 28 Strider64 User rank is Corporal (100 - 500 Reputation Level)Strider64 User rank is Corporal (100 - 500 Reputation Level)Strider64 User rank is Corporal (100 - 500 Reputation Level)Strider64 User rank is Corporal (100 - 500 Reputation Level) 
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

Reply With Quote
  #4  
Old March 8th, 2013, 09:14 PM
tjswebdev tjswebdev is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 69 tjswebdev User rank is Private First Class (20 - 50 Reputation Level)tjswebdev User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 14 h 5 m 2 sec
Reputation Power: 1
Send a message via Skype to tjswebdev
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?
PHP Code:
if($serverImg != none) { 


So like this?
PHP Code:
if(UPLOAD_ERR_NO_FILE !== 4){ // Because -UPLOAD_ERR_NO_FILE - Value: 4; No file was uploaded. 


Thanks.

Reply With Quote
  #5  
Old March 8th, 2013, 09:15 PM
tjswebdev tjswebdev is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 69 tjswebdev User rank is Private First Class (20 - 50 Reputation Level)tjswebdev User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 14 h 5 m 2 sec
Reputation Power: 1
Send a message via Skype to tjswebdev
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!

Reply With Quote
  #6  
Old March 9th, 2013, 08:25 AM
tjswebdev tjswebdev is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 69 tjswebdev User rank is Private First Class (20 - 50 Reputation Level)tjswebdev User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 14 h 5 m 2 sec
Reputation Power: 1
Send a message via Skype to tjswebdev
@ Strider64

Thanks. I ended up using your code! It works like a charm. Thanks

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - [PHP FORM] File Upload

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap