logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 07-21-2011, 12:38 AM   #1
FethrdWlf
FethrdWlf's Avatar
 
Join Date: Jan 2011
Location: Arizona
Posts: 146
Question Using header("Location: ...") after an FTP upload?...

I've made an upload script for a website my dad has so that he can upload pictures to the gallery and what not. Because the website is hosted via a web-hosting-website, I've decided to take advantage the FTP functions in PHP. While it isn't too important, I'd like to try and increase the efficiency of the process. The uploading part goes great. It's just that after I have uploaded the file, I'm not able to use the header("Location: ..."); line to go back to the upload page.

Again, it doesn't matter too much - but it would be nice to fix. Here's my upload script:
PHP Code:
<?php
    session_start
();
    include(
"../mysql.php");
    
        
//Upload and rename the image
        
$filep $_FILES['picture']['tmp_name'];
        
$filet $_FILES['thumb']['tmp_name'];
        
$ftpserver "ftp.mcneilcustomknives.x10.mx";
        
$ftpuser "...";
        
$ftppass "...";
        if(
$_FILES['picture']['type'] == "image/jpeg"){
            
$filetypep ".jpg";
        }else if(
$_FILES['picture']['type'] == "image/png"){
            
$filetypep ".png";
        }else if(
$_FILES['picture']['type'] == "image/gif"){
            
$filetypep ".gif";
        }
        if(
$_FILES['thumb']['type'] == "image/jpeg"){
            
$filetypet ".jpg";
        }else if(
$_FILES['thumb']['type'] == "image/png"){
            
$filetypet ".png";
        }else if(
$_FILES['thumb']['type'] == "image/gif"){
            
$filetypet ".gif";
        }
        
$name date("mdYis",time());
        
        
$conid ftp_connect($ftpserver);
        
$loginresult ftp_login($conid,$ftpuser,$ftppass);
        
        if((!
$conid) || (!$loginresult)){
            echo 
"FTP connection dropped.";
            exit;
        }else{
            echo 
"FTP connection lived.";
        }
        
        
$uploadp ftp_put($conid,"public_html/pictures/".$name.$filetypep,$filep,FTP_BINARY);
        
$uploadt ftp_put($conid,"public_html/pictures/thumbs/".$name.$filetypet,$filet,FTP_BINARY);
        if(!
$uploadp || !$uploadt){
            echo 
"FTP upload failed.<br />\n";
            exit;
        }
        
        
$_SESSION['imagelink'] = "http://mcneilcustomknives.x10.mx/pictures/".$name.$filetypep;
        
$_SESSION['thumblink'] = "http://mcneilcustomknives.x10.mx/pictures/thumbs/".$name.$filetypet;
            
        
//Do the rest
        
$title $_POST['title'];
        
$caption $_POST['caption'];
        
$imagelink $_SESSION['imagelink'];
        
$thumblink $_SESSION['thumblink'];
        
        
$query "INSERT INTO pictures (title,caption,url,thumburl) VALUES ('$title','$caption','$imagelink','$thumblink')";
        
$results mysqli_query($cnx,$query);
        
ftp_close($conid);
        
        
header("Location: index.php");
?>
Thanks for any help in advance!
FethrdWlf is offline   Reply With Quote
Old 07-26-2011, 12:10 PM   #2
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,256
Default

Ok first things first

Your code could do with some refactoring

This bit....

PHP Code:

if($_FILES['picture']['type'] == "image/jpeg"){ 
            
$filetypep ".jpg"
        }else if(
$_FILES['picture']['type'] == "image/png"){ 
            
$filetypep ".png"
        }else if(
$_FILES['picture']['type'] == "image/gif"){ 
            
$filetypep ".gif"
        } 
could be written like...

PHP Code:

switch($_FILES['picture']['type']){

case 
'image/jpeg' $filetypep ".jpg"; break;
case 
'image/png' $filetypep ".png"; break;
case 
'image/gif' $filetypep ".gif"; break;


The same goes for...

PHP Code:

if($_FILES['thumb']['type'] == "image/jpeg"){ 
            
$filetypet ".jpg"
        }else if(
$_FILES['thumb']['type'] == "image/png"){ 
            
$filetypet ".png"
        }else if(
$_FILES['thumb']['type'] == "image/gif"){ 
            
$filetypet ".gif"
        } 
PHP Code:

switch($_FILES['thumb']['type']){

case 
'image/jpeg' $filetypet ".jpg"; break;
case 
'image/png' $filetypet ".png"; break;
case 
'image/gif' $filetypet ".gif"; break;


Also never echo from within your logic. Assign your messages to a variable and output that. Keep your logic and design separate.

[bad practice...]
PHP Code:

  
if((!$conid) || (!$loginresult)){ 
            echo 
"FTP connection dropped."
            exit; 
        }else{ 
            echo 
"FTP connection lived."
        } 
Repeating elements will cause you a headache in the future

[example...]

Code:
public_html/pictures/

http://mcneilcustomknives.x10.mx/pictures/
These should be stored in a variable at the top of your script and the variable used instead.

Regarding your question

Does the script not redirect or do you get an error?
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk
davidj is offline   Reply With Quote
Old 07-26-2011, 11:09 PM   #3
FethrdWlf
FethrdWlf's Avatar
 
Join Date: Jan 2011
Location: Arizona
Posts: 146
Default

Quote:
Originally Posted by davidj View Post
Does the script not redirect or do you get an error?
Thanks for all the info! The script won't redirect and it gives me an error, saying that the headers have already been sent. Let me find the exact error... Here we are:

Warning: Cannot modify header information - headers already sent by (output started at /home/mcknives/public_html/gallery/script.php:31) in /home/mcknives/public_html/gallery/script.php on line 56

Here's my new script (after your suggested changes ):
PHP Code:
<?php
    session_start
();
    include(
"../mysql.php");
    
    if(isset(
$_POST['upload'])){
        
//Upload and rename the image
        
$filep $_FILES['picture']['tmp_name'];
        
$filet $_FILES['thumb']['tmp_name'];
        
$ftpserver "ftp.mcneilcustomknives.x10.mx";
        
$ftpuser "mcknives";
        
$ftppass "donter7514";
        switch(
$_FILES['picture']['type']){
            case 
'image/jpeg' $filetypep ".jpg"; break;
            case 
'image/png' $filetypep ".png"; break;
            case 
'image/gif' $filetypep ".gif"; break;
        }
        switch(
$_FILES['thumb']['type']){
            case 
'image/jpeg' $filetypet ".jpg"; break;
            case 
'image/png' $filetypet ".png"; break;
            case 
'image/gif' $filetypet ".gif"; break;
        }
        
$name date("mdYis",time());
        
        
$conid ftp_connect($ftpserver);
        
$loginresult ftp_login($conid,$ftpuser,$ftppass);
        
        if((!
$conid) || (!$loginresult)){
            echo 
"FTP connection dropped.";
            exit;
        }else{
            echo 
"FTP connection lived.";
        }
        
$path "public_html/pictures/";
        
$uploadp ftp_put($conid,$path.$name.$filetypep,$filep,FTP_BINARY);
        
$uploadt ftp_put($conid,$path."thumbs/".$name.$filetypet,$filet,FTP_BINARY);
        if(!
$uploadp || !$uploadt){
            echo 
"FTP upload failed.<br />\n";
            exit;
        }
        
        
$fullurl "http://mcneilcustomknives.x10.mx/pictures/";
        
$_SESSION['imagelink'] = $fullurl.$name.$filetypep;
        
$_SESSION['thumblink'] = $fullurl."thumbs/".$name.$filetypet;
            
        
//Do the rest
        
$title $_POST['title'];
        
$caption $_POST['caption'];
        
$imagelink $_SESSION['imagelink'];
        
$thumblink $_SESSION['thumblink'];
        
        
$query "INSERT INTO pictures (title,caption,url,thumburl) VALUES ('$title','$caption','$imagelink','$thumblink')";
        
$results mysqli_query($cnx,$query);
        
ftp_close($conid);
        
        
$_SESSION['itsdon'] = 1;
        
header("Location: index.php");
        
//echo "<br /><a href='index.php'>Go back to upload page.</a>&nbsp;<a href='../gallery'>Go to gallery page.</a>";
        
    
}elseif(isset($_POST['login'])){
        if(
$_POST['password'] == "donter7514"){
            
$_SESSION['itsdon'] = 1;
            
header("Location: index.php");
        }else{
            
$_SESSION['loginerror'] = 1;
            
header("Location: index.php");
        }
    }else{
        
session_destroy();
        
header("Location: index.php");
    }
?>
Don't worry about the last bit. I use the same script for several forms on one page. It keeps things neat on my server, and reduces space.
FethrdWlf is offline   Reply With Quote
Old 07-26-2011, 11:13 PM   #4
FethrdWlf
FethrdWlf's Avatar
 
Join Date: Jan 2011
Location: Arizona
Posts: 146
Default

Oooohhhh..... I got it. Apparently echoing something to the page before changing you location is a PHP no-no. So I just deleted "echo "FTP connection lived." and it works fine now... -embarrassed face-
FethrdWlf is offline   Reply With Quote
Reply


Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 12:38 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com