Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File_exists

Status
Not open for further replies.

partymong

Programmer
Nov 5, 2003
39
0
0
GB
Hi All,
I have a remote ftp server, which is specified as a virtual directory(unc path) on my webserver.

I want to check if a file exists on the ftp server/my webserver virtual directory (unc path) before I run a script to upload a file. (basically I want to throw out a message if the filename from my upload form already exists on the ftp server).

I have tried using file_exists but it did not seem to work..

Any help would be appreciated

Thanks,

P
 
Hi sleipnir214,
Yes I'm running php on win32 IIS 5.
The ftp server is on the same machine, the ftp data resource is on a remote server.

I have a file that does exists but my code doesn't catch it...

perhaps I've made a schoolboy error....

Heres my code:

if ($_FILES['file1'] != "")
{
if ($_FILES['file1']['type'] == "application/x-zip-compressed")
{
if (file_exists('/uploaded/testdata/DB1.zip'))
{
echo "The file {$_FILES['file1']['name']} already exists on the Resource, If the file is different, please rename the file";
}
else
{
$ftpServer = "myserver";
$ftpUser = "myuser";
$ftpPass = "mypassword";

$conn = @ftp_connect($ftpServer) or die ("Couldn't connect to FTP server");
$login = @ftp_login($conn, $ftpUser, $ftpPass) or die ("Login details failed !");
$putFile = @ftp_put($conn, $_FILES[file1][name],$_FILES[file1][tmp_name], FTP_BINARY);

ftp_quit($conn);
$cookie_life = 3*24*3600;
Setcookie("blah",$_FILES['file1']['name'],time()+$cookie_life);
header("Location: false);
}
}
else
{
die ("Wrong file type only .zip files allowed!");
}
}
else
{
die ("No input file specified.");
}
?>

<html>
<head>
<title>File Upload</title>
<body>
<?
if ($putFile)
{
//echo &quot;File upload OK.&quot;;
echo &quot;File {$_FILES['file1']['name']} uploaded OK&quot;;
}
else
{
echo &quot;Upload of file {$_FILES['file1']['name']} failed.&quot;;
}
?>
<form action=&quot;upload_form.html&quot;>
<p><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Back to upload form&quot;></p>

</body>
</head>
</html>
 
I'm confused.

You have an IIS machine running PHP. You have a PHP script that will accept a file upload. When that script gets a file, it looks on the local machine for an existing file, and if it does not exist uploads that file to another machine via FTP?

Want the best answers? Ask the best questions: TANSTAAFL!!
 

Ok,

The ftp server (task) is running on the same machine as the web server and php. But the ftp server is pointing to a directory on a remote server. (default path)

i.e. The path of the ftp server is on a seperate machine to where the server is running. e.g My ftp server is on server : ftpserver
The directory path for ftpserver (file uploads) is server: ftpdata

In IIS I have created a 'virtual directory' /uploaded/testdata which is a unc path to server : ftpdata.

I hope that clears things up...


P
 
There's the problem. PHP's filesystem functions operate at the entire filesystem level, not in terms of virtual directories in IIS.

You're going to have to point file_exists() to the actual filesystem directory.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Ok Great! Thanks sleipnir214.

Is there any other way of verifying that the file exists, other than using file_exists?

I found this script....

$myUrl = &quot;# parse the URL so you get the parts you need
$fsPara = parse_url($myUrl);
# open a socket connection to the server
$fID = fsockopen($fsPara[&quot;host&quot;], 80);
if (!fID){
echo(&quot;Could not connect&quot;);
} else {
# use a HEAD header to see if it returns status code 200
fputs($fID, &quot;HEAD $fsPara[&quot;path&quot;] http/1.0\r\nHost: $fsPara[&quot;host'}\r\n\r\n&quot;);
# read the response
$head = fread($fID, 4096);
fclose($fID);
# check for status 200 in the header
if (!preg_match('#^http/.*\s+200\sOK\s#i', $head)){
# ... no file;
} else {
# file was accessible through http
}
}

but it errors with the following:-

//# ... no file;
echo (&quot;no file&quot;);

Parse error: parse error, unexpected '&quot;', expecting T_STRING or T_VARIABLE or T_NUM_STRING in c:\inetpub\ on line 11

Any ideas?

Thanks,

P
 
I don't know why you'd be going through all the trouble of trying to use HTTP head requests to find the file.

If the actual machine and directory where the file exists is visible from the Win32 PHP host machine, then a line like:

file_exists('//machinename/path/to/file.txt')

will work.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Ok brilliant! Thanks for your help!

I'll give it a try...

P
 
sleipnir214,

I still can't get the code to work.

Refrerring to the following....
If the actual machine and directory where the file exists is visible from the Win32 PHP host machine

What do you mean by 'visible'?

What 'user' would need access to the file?


Thanks,

P


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top