How do upload a file from one PC to another PC using PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tamileelam
Forum Newbie
Posts: 1
Joined: Thu Sep 06, 2007 8:56 pm

How do upload a file from one PC to another PC using PHP?

Post by tamileelam »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi!


   I tried to upload a file from one pc to another pc using PHP.
I didnt able to upload that file. But, I was able to upload a file  
to FTP or local directory. Can you provide the PHP script for
upload a file to remote PC?. I have attached the sample code.
Please correct that code.

    Thank you verymuch!

Tamileelam.

SourceCode :  upload.html
 ----------------------------------

[syntax="html"]<html>
<head></head>
<body>
<h2>Please provide the following information:</h2>

<form enctype="multipart/form-data" method="post" action="upload.php">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
Host <br />
<input type="text" name="host" /><p />

Username <br />
<input type="text" name="user" /><p />

Password <br />
<input type="password" name="pass" /><p />

Destination directory <br />
<input type="text" name="dir" /><p />

File <br />
<input type="file" name="file" /><p />

<input type="submit" name="submit" value="Upload File" />
</form>

</body>
</html>

SourceCode :upload.php
-------------------------------[/syntax]

Code: Select all

<?php

// get FTP access parameters

$host = $_POST['host'];

$user = $_POST['user'];

$pass = $_POST['pass'];

$destDir = $_POST['dir'];

$workDir = "C:/"; // define this as per local system

// get temporary file name for the uploaded file

$tmpName = basename($_FILES['file']['tmp_name']);

// copy uploaded file into current directory

move_uploaded_file($_FILES['file']['tmp_name'], $workDir."/".$tmpName) or die("Cannot move uploaded file to working directory");

// open connection

$conn = ftp_connect($host) or die ("Cannot initiate connection to host");

// send access parameters

ftp_login($conn, $user, $pass) or die("Cannot login");

// perform file upload

$upload = ftp_put($conn, $destDir."/".$_FILES['file']['name'], $workDir."/".$tmpName, FTP_BINARY);

// check upload status

// display message

if (!$upload) {

    echo "Cannot upload";

} else {

    echo "Upload complete";

}

// close the FTP stream

ftp_close($conn);

// delete local copy of uploaded file

unlink($workDir."/".$tmpName) or die("Cannot delete uploaded file from working directory -- manual deletion recommended");

?>




feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Why do you need to FTP it?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

If the PC is on the same network you just need the destination to have 777 permissions set and do \\PC NAME\Location
Post Reply