I have already made an upload form. But I want users to be able to upload .zip files and .rar files. I already have tried the following:
$ftp_server = "myserver";
$ftp_user_name = "myuser";
$ftp_user_pass = "mypass";
$source_file = $_FILES['file']['tmp_name'];
$fileType = $_FILES['file']['type'];
$fileExt = explode('.', $filename);
$fileActualExt = strtolower(end($fileExt));
$destination_folder = "/public_html/wp/wp-content/plugins/AbonneerProgrammas/FilesUpload";
$destination_file = $destination_folder . "/" . basename($_FILES['file']['name']);
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
$allowed = array('zip', 'rar');
if(in_array($fileActualExt, $allowed)) {
//my upload code
}else {
echo "You are not allowed to upload this file.";
}
It does not show any errors. It only enters inside the else. It also does this when I am uploading .zip files, I do not know what is wrong.
1.You need to use pathinfo() to get file extension.
2.$filename is no where defined in your code, so it will give you warning as well as extension checking code will always fail.
Modify code like below:
$ftp_server = "myserver";
$ftp_user_name = "myuser";
$ftp_user_pass = "mypass";
$source_file = $_FILES['file']['tmp_name'];
$fileType = $_FILES['file']['type'];
/*get file extension [*/
$fileExt = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
$fileExt = strtolower($fileExt);
/* ] */
$destination_folder = "/public_html/wp/wp-content/plugins/AbonneerProgrammas/FilesUpload";
$destination_file = $destination_folder . "/" . basename($_FILES['file']['name']);
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
$allowed = array('zip', 'rar');
/*use $extension variable to check allowed or not [*/
if(in_array($fileExt, $allowed)) {
//my upload code
}else {
echo "U mag dit type bestand niet uploaden.";
}
/* ] */
Related
This question already has an answer here:
ftp_put is corrupting my movies after transfer, is it wrong?
(1 answer)
Closed 3 days ago.
I am working on a self taught local project and trying to upload a file to a server.
but when I upload the image file it gets corrupted.
this is my upload.php
<?php
$ds = DIRECTORY_SEPARATOR; //1
$storeFolder = 'uploads'; //2
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name']; //3
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds; //4
$targetFile = $targetPath. $_FILES['file']['name']; //5
move_uploaded_file($tempFile,$targetFile); //6
$ftp_server="myServer";
$ftp_user_name="userName";
$ftp_user_pass="Password";
$file = $targetFile;//tobe uploaded
$remote_file = $_FILES['file']['name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
// close the connection
ftp_close($conn_id);
}
?>
It currently uploads to the local folder Uploads also to the server but corruptted any help on this will be great.
Have you tried using FTP Binary? an image is a binary file not an ASCII file.
enter image description here
filepath:C:\TEMP\bcproject\images
I have tested the following code below. When I tried to upload an image in my directory path C:\TEMP\bcproject\images, the image successfully insert into the database. But the problem I faced is I couldn't view the image at my folder...
Above I did provide the picture of the problem.
Seeking for help now
my code:
testaddimage.php
<?php
include("connection.php");
if(isset($_POST['but_upload'])){
$name = $_FILES['file']['name'];
$target_dir = "images/" .$name;
$target_file = $target_dir . basename($_FILES["file"]["name"]);
// Select file type
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Valid file extensions
$extensions_arr = array("jpg","jpeg","png","gif");
// Check extension
if( in_array($imageFileType,$extensions_arr) ){ // this can but no image appear
// Insert record
$query = "insert into test(imagename) values('".$name."')";
mysqli_query($conn,$query);
// Upload file
move_uploaded_file($_FILES['file']['tmp_name'],$target_dir);
}
/*$name = $_FILES['file']['name'];
$tmpname = $_FILES['file']['tmp_name'];
$size = $_FILES['file']['size'];
$error = $_FILES['file']['error'];
$type = $_FILES['file']['type'];
echo $name. " ". $tmpname. " ".$size . " ". $error . " ". $type;
$fileext = (explode('.',$name));
$fileactualext = strtolower(end($fileext));
$allowed = array("jpg","jpeg","png","gif");
if( in_array($fileactualext,$allowed) ) {
if ($error === 0)
{
if($size < 1000000){
$filenamenew = uniqid('',true). "." . $fileactualext;
$filedir = 'images/'. $filenamenew;
move_uploaded_file( $tmpname,$filedir );
header("location:testaddimage.php");
} else {
echo "File to big";
}
echo "There was an error.";
}
}else
{
echo "cannot uploade";
}
*/
}
?>
<form method="post" action="testaddimage.php" enctype='multipart/form-data'>
<input type='file' name='file' />
<input type='submit' value='Save name' name='but_upload'>
</form>
connection.php
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$database = "playgadget";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
First, does move_uploaded_file return true or false ? (https://www.php.net/manual/fr/function.move-uploaded-file.php)
Second, have you checked the target directory permissions ?
Third, as you are on Windows, be sure that slashes are in good (use DIRECTORY_SEPARATOR const instead of "/" or "\"). I don't remember if php take care of this or not.
I want to upload multiple files using ftp in php. Just want to make sure that I am doing it correctly or the good way. I am able to upload the file on my localhost but after I transfer the project to the server, It takes time to upload.
Sometimes, it also give an error unable to connect to ftp if I upload more than 1 files. Below are the codes that I did.
for($i=0; $i < count($_FILES['uploaded']['name']); $i++){
$array = array($_FILES['uploaded']['name']);
$tmpFilePath = $_FILES['uploaded']['tmp_name'][$i];
$filesize = $_FILES['uploaded']['size'][$i];
if($filesize > 10485760){
$msgError[] = "Exceed maximum file size!";
}
if($tmpFilePath != ""){
$shortname = $_FILES['uploaded']['name'][$i];
$filePath = $date.'-'.$_FILES['uploaded']['name'][$i];
// Start FTP Connection
require "../ftp-conn.php";
$file = $tmpFilePath;
$remote_file = "/public_html/procurement/uploads/" . $filePath;
// turn passive mode on
ftp_pasv($conn_id, true);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
echo "successfully uploaded $file\n";
} else {
die ("There was a problem while uploading $file\n");
}
// close the connection
ftp_close($conn_id);
}
}
Is there a better solution for this?
Update codes:
// Start FTP Connection
$ftp_server = "ftp.myweb.com";
$ftp_user_name = 'user';
$ftp_user_pass = 'password';
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or
die("Couldn't connect to $ftp_server");
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// turn passive mode on
ftp_pasv($conn_id, true);
//Function to check file size and upload file if success
for($i=0; $i < count($_FILES['uploaded']['name']); $i++){
$tmpFilePath = $_FILES['uploaded']['tmp_name'][$i];
$filesize = $_FILES['uploaded']['size'][$i];
if($filesize > 10485760){
$msgError[] = "Exceed maximum file size!";
}
if($tmpFilePath != ""){
$shortname = $_FILES['uploaded']['name'][$i];
$filePath = $date.'-'.$_FILES['uploaded']['name'][$i];
$file = $tmpFilePath;
$remote_file = "/public_html/procurement/uploads/" . $filePath;
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
echo "successfully uploaded $file\n";
} else {
die ("There was a problem while uploading $file\n");
}
}
}
// close the connection
ftp_close($conn_id);
I have problem with my image uploade, it doesnt work...
give me this error
Connected to ftp.exemple.com, for user exemple#exemple.com Warning:
ftp_put(): Filename cannot be empty in
/home/user/public_html/foto-test.php on line 26 FTP upload has
failed!
<?php
$ftp_server = "ftp.xxxx.com";
$ftp_user_name = "xxxx#xxxx.com";
$ftp_user_pass = "xxxxxxx";
$folder = "/public_html/images";
$destination_file = $folder . $_FILES['file']['name'];
$source_file = $_FILE['file']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); //line 26
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>
I tried to change the $folder destination but it give me the some error...
Check out the answers here PHP FTP upload error: Warning: ftp_put() [function.ftp-put]: Filename cannot be empty in
Also your destination path should have a slash at the end
$folder = "/public_html/images";
$destination_file = $folder . $_FILES['file']['name'];
change that to
$folder = "/public_html/images/";
$destination_file = $folder . $_FILES['file']['name'];
So it will be clear that whatever(image) you uploading is going into the image folder and not 'image+SomeName' folder
I have a problem with the file type check when uploading on server. My function is not working as it should. On the server is always uploaded absolutely everything. Please help me
<?php
session_start();
include_once 'dbconnect.php';
if (isset($_POST['ulozitzmeny'])) {
$valid_mime_types = array(
"image/gif",
"image/png",
"image/jpg",
"image/jpeg",
);
if (in_array($_FILES["file"]["type"], $valid_mime_types)) {
$file = rand(1000, 100000) . "-" . $_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder = "images";
$new_size = $file_size / 1024;
$new_file_name = strtolower($file);
$final_file = str_replace(' ', '-', $new_file_name);
if (move_uploaded_file($file_loc, $folder . $final_file)) {
$sql = "UPDATE users SET file='$file', type='$file_type', size='$file_size' WHERE username = '$_SESSION[user]'";
mysql_query($sql);
}
}else{
echo 'error';
}
}
?>
There's a much easier way to validate the type of file being uploaded. Use fileinfo to get the extension of the file being uploaded and then compare against permissible file extensions.
Here's the reference:
fileinfo
Your code should be like this:
// your code
// valid file extensions
$valid_extensions = array("gif", "png", "jpg", "jpeg");
// get the file extension
$ext = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION)); // png
// now check against permissible extensions
if(in_array($ext, $valid_extensions)){
// allowed
}else{
// not allowed
}
// your code