PDA

View Full Version : Using header("Location: ...") after an FTP upload?...


FethrdWlf
07-20-2011, 11:38 PM
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
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! 8-)

davidj
07-26-2011, 11:10 AM
Ok first things first

Your code could do with some refactoring

This bit....



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...



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...



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";
}



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...]


if((!$conid) || (!$loginresult)){
echo "FTP connection dropped.";
exit;
}else{
echo "FTP connection lived.";
}

Repeating elements will cause you a headache in the future

[example...]



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?

FethrdWlf
07-26-2011, 10:09 PM
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
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_B INARY);
$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
07-26-2011, 10:13 PM
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-