Jump to content

Anything wrong with this?


FlyingIsFun1217

Recommended Posts

Hey!

 

Need more code help. I can't seem to upload anything to my server (would be the ftp_put function if anything).

 

Here's what I have:

 

<?php include("header.php"); ?>

<form action="uploadImage.php" method="post"/>
<center>
	<p>
		<b>Image to Upload:</b>
		<input type="file" name="imageLocalLoc" style="color: #383838; background-color: #46C74A;"/>
	</p>

	<br>

	<p>
		<b>Image Name (No Extension Needed):</b>
		<input type="text" name="imageName" style="color: #383838; background-color: #46C74A;"/>
	</p>
</center>

<center>
	<input type="submit" value="Upload Image!">
</center>
</form>

<?php include("footer.php"); ?>

 

and uploadImage.php:

 

<?php
$ftp_server = "flyingisfun1217.freeweb7.com";
$ftp_user_name = "flyingisfun1217";
$ftp_user_pass = "my_ftp_password_here";

$pictureName = $_POST['passwrd'];
$ftpName = $_POST['imageName'];

$ext = substr($pictureName, strrpos($fileName, '.') + 1);

$serverConnection = ftp_connect($ftp_server);

$login_result = ftp_login($serverConnection, $ftp_user_name, $ftp_user_pass);

if ((!$serverConnection) || (!$login_result))
{
        echo "FTP connection has <b>failed!</b>";
echo "<br>";
        echo "Attempted to connect to ".$ftp_server." for user ".$ftp_user_name;
echo "<br>";
        exit;
}
else
{
echo "FTP connection has <b>succeeded!</a>";
echo "<br>";
        echo "Connected to ".$ftp_server.", for user ".$ftp_user_name.".";
echo "<br>";
}

$upload = ftp_put($serverConnection, $ftpName.".jpg", $pictureName, FTP_BINARY);

if (!$upload)
{
echo "FTP upload of ".$ftpName.".jpg has failed!";
}
else
{
        echo "Uploaded $source_file to $ftp_server as $destination_file";
}

ftp_close($serverConnection);
?> 

 

Every time I try and run it, it gives me my dreaded "FTP upload of Image.jpg has failed!". What could be wrong here?

 

Thanks again!

FlyingIsFun1217

Link to comment
https://forums.phpfreaks.com/topic/102026-anything-wrong-with-this/
Share on other sites

Well, the ftp_put() function takes a local file, and uploads it to an ftp server. The file in question isn't local to the server, it's local to the user's machine.

 

You don't need FTP functions for this. PHP can handle uploads itself. Take a look at the manual page:

 

http://uk.php.net/manual/en/features.file-upload.php

 

There's a full example of an upload form there.

The only reason I wanted to do an FTP upload is because it seems my hosting provider has not allowed php to upload to the server.

 

There's no way of doing an FTP upload through a remote (user's PC) machine? Seems like it would be such a trivial thing.

 

Thanks again for your time!

FlyingIsFun1217

Replace:

<form action="uploadImage.php" method="post"/>

With:

<form enctype="multipart/form-data" action="uploadImage.php" method="post"/>
<input type="hidden" name="MAX_FILE_SIZE" value="30000">

 

Fill MAX_FILE_SIZE with the appropriate value (in bytes), and try it again.

  • 2 weeks later...

I know it's been a while, but that didn't work

 

upload.php:

<?php include("header.php"); ?>
<form enctype="multipart/form-data" action="fileUp.php" method="post">
	<input type="hidden" name="MAX_FILE_SIZE" value="10485760">
	<center>
		<input type="file" name="fileForUp">
		<br>
		<input type="submit" value="Upload">
	</center>
</form>
<?php include("footer.php"); ?>

 

fileUp.php

<?php
$fileToUpload = $_POST['fileForUp'];
$targetPath = "/".basename($_FILES['fileForUp']['name']);

if(move_uploaded_file($_FILES['fileForUp']['name']))
{
	include("header.php");
	echo '<center>';
		echo 'File successfully uploaded!';
		echo '<br>';
		echo '<a href="javascript:history.go(-1)">Return to Upload Form</a>';
	echo '</center>';
	include("footer.php");
}

else
{
	include("header.php");
	echo '<center>';
		echo 'Unsuccessful file upload!';
		echo '<br>';
		echo '<a href="javascript:history.go(-1)">Return to Upload Form</a>';
	echo '</center>';
	include("footer.php");

}
?>

 

Thanks again!

FlyingIsFun1217

Well, the only reason I mention it at all now is because searching through their forums, others have been able to upload through php. I just wanted to check to make sure it was all correct before I started complaining ;)

 

And switching hosts would be hard. I'd need something comparable to what I've got now (free, 8 GB, 100 GB bandwidth), just with these functions allowed.

 

FlyingIsFun1217

They've actually lasted quite a while, and while I can't predict the future, I can see them lasing for quite a while (they also do paid hosting).

 

Right now though, as I've said, it has to be free. There is simply no way for me to do paid hosting yet (under 18, and parents would not want to pay for that; neither would I).

 

Thanks again!

FlyingIsFun1217

It's possible that your host only allows PHP uploads for paying members, which might explain the forums posts.

 

Either way, you can check if your host is allowing PHP uploads by looking at the output of phpinfo(); One of the options under PHP core is file_uploads.

 

As for your code one thing immediately jumps out at me. The move_uploaded_file() function requires two parameters. You only give it one.

 

See the manual for a short, simple example of a PHP file upload script.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.