Wrox Programmer Forums
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 31st, 2004, 11:13 AM
Authorized User
 
Join Date: Jun 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Uploading Files

Hi,
I was hoping to find a solution to this problem, I would like to upload files from my machine (say My Documents) to a default images folder on the server? Is this possible using PHP, I don't want to insert them into the database just make them available for a PHP script to display the correct image like this:

<img src=\"images/$row_Recordset1[itemId].jpg\" width=\"50\" height=\"50\" border=\"1\" alt=\"Click for full details..\">

I am using an FTP program at the moment but wondered if there is a way I can do it via a form like:

<form enctype="multipart/form-data" method="POST">

I have tried this several ways and had no success, first I was getting a no image selected error, and then getting, a permissions error. So I am thinking that it is my host that is preventing me from doing this??

Thankyou
Sami
 
Old March 31st, 2004, 04:51 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

There are a few ways of doing this:

1.) Upload the image and use FTP functions to copy the image to another folder (gets around the permissions thing).
2.) Directly copy the image to another folder.

Personally I'd recommend method number one. This requires FTP functions to be enabled -- these are enabled by default on Windows but on Linux PHP must be compiled with the option. Check phpinfo() to see if they are enabled.

http://www.php.net/ftp

Then all you're doing is storing the filename that you create in the database. Otherwise you must set permissions on the directory. What OS are you using, Windows, Linux?



: )
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
 
Old April 4th, 2004, 07:44 PM
Authorized User
 
Join Date: Jun 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your help Rich but, unfortunately I cannot get any script to work as I have tried various types and get errors from

Call to undefined function: ftp_connect()

to a blank screen saying the image file has been uploaded but I cannot find the file, I want customers who have logged in to be able to upload their files, as I can do it fine with an Ftp application but it is more for clients to log in and upload images of their choice to the images directory.

I have checked phpinfo and it says file uploads = 1, so I think my host is allowing uploads but I have been trying to do this for ages without success.

My host is running Unix, Apache and PHP (ver 4.0.6), the script I have been trying is:


<?php

echo '<form action="image_upload.php" method="post" enctype="multipart/form-data">';
echo 'Click the Browse button to find the file you wish to upload';
echo '<input type="file" name="imagefile">';
echo '<INPUT TYPE="submit" name="upload" value="upload">';
echo '</form>';

$ftp_user_name='***********';
$ftp_user_pass='********';
$ftp_server='www.mydomain.co.uk';
$ftp_dir='/web/';

$web_dir='../images/';
$web_location=$web_dir.$imagefile_name;

$destination_file=$ftp_dir.$imagefile_name;


$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$upload = ftp_put($conn_id, $destination_file, $imagefile, FTP_BINARY);


$ch=ftp_site($conn_id,"chmod 777 ".$destination_file);
ftp_close($conn_id);


if (file_exists($web_location))
    {
    echo "file was uploaded as $web_location";
    }
else
    {
    echo "Could not create $web_location";
    }


?>


I don't know why I am getting the ftp_connect() error, but would really like to get this problem sorted out as the application I have built would then not need any third party software to run their own site, including uploading their own images. Is the script I am using no use and should I start again from scratch?

Thankyou in adavance.
Sam
 
Old April 4th, 2004, 08:25 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Well it doesn't sound like you have FTP functions enabled. When you run phpinfo() there is a heading that says FTP and under it it says enabled. If they are disabled then the heading won't be there at all. You probably don't anyway, since your ISP is unwilling to upgrade PHP, they probably don't have any extra options enabled either.

It sounds like you're going to have to directly copy the file to the directory, all this requires is setting permissions on the destination directory. Are you able to login to the web server's shell to make chmod commands? If not -- can you download and use WS FTP, an FTP program that supports making simple shell commands like chmod.

: )
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
 
Old April 5th, 2004, 04:18 AM
Authorized User
 
Join Date: Jun 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Rich,
Well, here we are again, more problems cos of my ISP. No, there is no Ftp heading at all under phpinfo(), sigh. I cannot login under shell but I have used WS FTP to change permissions before.

I am unsure if they will let me change the permissions though, but I can try...

Sami
 
Old April 6th, 2004, 01:34 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The other simple answer is to ask your ISP if they've incorrectly set the permissions of the directory that they've specified as PHP's upload temp dir. You should feel free to modify the permissions or ownership of your user account directories and files.

If the ISP didn't want users to use file uploading features, they would've disabled it in php.ini...


Take care,

Nik
http://www.bigaction.org/
 
Old April 16th, 2004, 09:09 AM
Authorized User
 
Join Date: Jun 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
Well I have finally done it, I have upgraded my ISP and hopefully I will solve all the problems I had with ftp'ing files as my new ISP has ftp uploads enabled and is running PHP 4.3.5, so now I can correct all my code from $HTTP_*_VARS to $_POST in preparation for PHP 5!

I still haven't ftp any files yet (ha ha) but at least I now know (thanks to all the great responses from you guys) I now have the right hardware to do it.
Thank you all very much
Sami
 
Old April 16th, 2004, 02:29 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Quote:
quote:Originally posted by Sami:

Well I have finally done it, I have upgraded my ISP..
Right on! :)

-Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::





Similar Threads
Thread Thread Starter Forum Replies Last Post
uploading files vinoskiboyetski Beginning PHP 1 November 28th, 2007 02:30 PM
uploading files rj1406 ASP.NET 1.0 and 1.1 Basics 1 October 11th, 2004 10:27 AM
Uploading files mildge Beginning PHP 6 December 1st, 2003 12:42 PM
uploading files by c# rborsatti C# 2 September 29th, 2003 03:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.