<?php
$ftp_server = "ftp.server.url";
$ftp_user = "user";
$ftp_password = "password";
$file_to_upload = "localfile.txt";
$remote_location = "/location/file.txt";
// set up connection or exit with message
$flink = ftp_connect($ftp_server) or exit("Can't connect to ftp server: $ftp_server");
// login or at least try
if(ftp_login($flink, $ftp_user, $ftp_password)) {
// if login successful use ftp_put to upload the file
// if you upload binary files use mode FTP_BINARY
if(ftp_put($flink, $remote_location, $file_to_upload, FTP_ASCII)) {
echo "Success! File is uploaded!";
} else {
echo "Can't upload file";
}
} else {
echo "Can't login with this user & password";
}
// close the connection
ftp_close($flink);
?>
You can get more information at http://www.php.net/manual/en/ref.ftp.php
Java, JSP, JSF, Struts, Hibernate and Ajax from an everyday life. Nothing serious just some thoughts.
Sep 9, 2011
Fast PHP example - FTP file upload
Here is one fast and simple example on how to do FTP file uploads with PHP.
Aug 19, 2011
PHP : Handling file uploads - easy tutorial with example
Handling file uploads with PHP is very easy task and here you will learn how to do it. And because I believe that people learn best with examples lets start with this.
Now lets upload one file to some user directory on a server. Lets create file upload_form.php on our server. It will contain mainly html code:
upload_form.php
Its very simple file with only one form. Don’t forget to add enctype="multipart/form-data" to your form when its used for file uploads. In this form we have to fields - the first is our file and the second is the submit button.
Now we need another file upload.php. Here is its content
upload.php
Here we have destination directory for our uploads - $dir = '/var/www/files/'. Our file will be saved in this directory and will have the name of the uploaded file.
The function move_uploaded_file checks if the file (first parameter) is a valid uploaded file and if its valid it moves it to $destinationfile. If you just want to check the file without moving it use is_uploaded_file instead.
And that’s all. Now if you have done everything the right way you must see “Upload successful!” when you use upload_file.php and upload file.
The original of this tutorial is here http://ldeveloper.blogspot.com/2011/08/php-and-handling-file-uploads-easy.html
Now lets upload one file to some user directory on a server. Lets create file upload_form.php on our server. It will contain mainly html code:
upload_form.php
<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
File: <input name="ourfile" type="file" />
<button type="submit">Upload</button>
</form>
</body>
</html>
Its very simple file with only one form. Don’t forget to add enctype="multipart/form-data" to your form when its used for file uploads. In this form we have to fields - the first is our file and the second is the submit button.
Now we need another file upload.php. Here is its content
upload.php
<?php
$dir = '/var/www/files/';
$destinationfile = $dir . basename($_FILES['ourfile']['name']);
if (move_uploaded_file($_FILES['ourfile']['tmp_name'], $destinationfile)) {
echo "Upload successful!";
} else {
echo "File attack!\n";
}
?>
Here we have destination directory for our uploads - $dir = '/var/www/files/'. Our file will be saved in this directory and will have the name of the uploaded file.
The function move_uploaded_file checks if the file (first parameter) is a valid uploaded file and if its valid it moves it to $destinationfile. If you just want to check the file without moving it use is_uploaded_file instead.
And that’s all. Now if you have done everything the right way you must see “Upload successful!” when you use upload_file.php and upload file.
The original of this tutorial is here http://ldeveloper.blogspot.com/2011/08/php-and-handling-file-uploads-easy.html
Aug 16, 2011
PHP: Playing with StdClass
What is StdClass and where does it live?
StdClass is a document class in PHP with no predefined members. Its the class that is created when you cast something to object like this:
$obj = (object) $something;
Sounds good but how to get its members if we don’t know about them? Actually, its very easy and we have several approaches.
First, we can iterate through its members with foreach:
$array = (array) $obj;
Or get its members as array:
get_object_vars($obj);
What else we can do with StdClass?
We can create objects and assign members:
Catchable fatal error: Object of class stdClass could not be converted to string
the culprit is our stdClass of course. Maybe you are trying to print an object of this class.
Read how to use SOAP with PHP
StdClass is a document class in PHP with no predefined members. Its the class that is created when you cast something to object like this:
$obj = (object) $something;
Sounds good but how to get its members if we don’t know about them? Actually, its very easy and we have several approaches.
First, we can iterate through its members with foreach:
foreach ($obj as $key => $value) {
}
Second we can cast our object to array:$array = (array) $obj;
Or get its members as array:
get_object_vars($obj);
What else we can do with StdClass?
We can create objects and assign members:
$std = new StdClass(); $std->key = "value";and access them:
echo $std->key;If you see an error like this:
Catchable fatal error: Object of class stdClass could not be converted to string
the culprit is our stdClass of course. Maybe you are trying to print an object of this class.
Read how to use SOAP with PHP
Subscribe to:
Posts (Atom)
Popular Posts
-
I’ll continue my article about JQuery Effects - with this one. It would be short information on how to make custom JQuery slideshow animati...
-
This tutorial tries to provide fast and easy instructions on getting started with Hibernate. You can download Hibernate from http://www.hibe...
-
I receive this nasty error yesterday and it took me some time to figure out the problem. This line passes without any problems $client = n...
-
JMesa is a very useful open source project.It is a dynamic HTML table that allows you to edit, filter, sort, paginate and export your data....
-
This would be simple and easy tutorial on how to use Apache Struts in Java web projects. What do you need for this tutorial? Java Tomcat...
-
Here is one fast and simple example on how to do FTP file uploads with PHP. < ? php $ ftp_server = "ftp.server.url" ; $ ...
-
This question is somewhat painful for me. Maybe I am too stupid. Anyway, if you want to make tar archive without using exec, for example on ...
-
JQuery is a lightweight JavaScript framework that got a lot of attention lately so I decided to write few rows about it. This post-tutorial...
-
You can use JQuery Effects in many different ways. I'll write few examples just to show how cool and fun is to work with this API. Usi...
-
Using SOAP with PHP can be very easy if you do everything right. First your PHP must be configured with --enable-soap. With Windows and WAMP...