Sry for asking so much [ 3rd in 2 days ]
this time I want to make it so when ftp_put uploads a file it wont show the result each time it uploads , like "Success Success" but I want it to say only when it finishes to upload all the files.
My current code:
foreach (glob("black/*") as $filename)
if(ftp_put($conn, $ftpFolder . basename($filename) , $filename, FTP_BINARY)) {
echo "sall goodman"; }
else { echo "not goot bruh"; }
}
[ dont look at the answers right now, its just for duh lulz and testing ]
Thanks in advance guys, You are really helping me out :)
Just comment the statement , echo "sall goodman" and you are good to go such that you won't get the repeated success message.
I have extended your example a bit, If all files are uploaded successfully you get the message All files have been successfully uploaded , else say if some 2 files failed on upload then you will get 2 file(s) have been failed during upload.
$totFiles = 0;
$successUploadFiles=0;
foreach (glob("black/*") as $filename)
if(ftp_put($conn, $ftpFolder . basename($filename) , $filename, FTP_BINARY)) {
//echo "sall goodman";
$successUploadFiles++;
}
else { echo "not goot bruh"; }
$totFiles++;
}
if($totFiles == $successUploadFiles)
{
echo "All files have been successfully uploaded";
}
else
{
echo $totFiles-$successUploadFiles." file(s) have been failed during upload";
}
Related
On a PHP page, I call an upload function that contains the standard PHP upload procedure. After I call the function, I do a redirect (tried with either window. location or header ()).
The strange thing is that everything works fine a couple of times, then it would not upload anymore (uploadOK won't be 0 either). It would just not move the file onto the server.
Then, I would take out the redirect and the upload would start working again. I put the redirect back in, the upload will still work a couple of times then stop again... Do you have any idea why?
Another strange thing is that, even when it doesn't work, the upload function still returns the correct path+filename, but it would not echo "File ... was uploaded".
I suspect that the problem might be in the move_uploaded_file() function... but it would not return 0, because "Error..." would not be echoed.
Without calling the redirect after the upload, it uploads fine every time.
PHP page:
$_SESSION["temp_file_name"]="../".UploadFisier($_FILES["fileToUpload"], $_SESSION["ID_CLASA"]."_".$id_item."_temp_".UserIdLogat($dbocr)."_", "../teme/", "");
header("Location: trimite_tema_script.php");
The Upload function:
function UploadFisier($file, $sufix, $target_dir, $maxsize)
{
if ($maxsize=="") { $maxsize=3000000; }
if ($target_dir=="") { $target_dir="../upload/"; }
$target_empty_file=$target_dir.$sufix;
$target_file = $target_empty_file.basename($file["name"]);
$uploadOk = 1;
if ($target_file != $target_empty_file)
{
if ($file["size"] > $maxsize)
{
echo "file too large";
$uploadOk = 0;
}
}
else
{
echo "no file selectec.";
$uploadOk = 0;
}
if ($uploadOk == 1)
{
//we overwrite
if (file_exists($target_file))
{
unlink($target_file);
}
if (move_uploaded_file($file["tmp_name"], $target_file))
{
echo "File ". basename( $file["name"]). " was uploaded.";
//we output the path and filename without the "../" at the beginning
$linksave=substr($target_file, 3);
}
else
{
echo "Error....";
}
}
echo "uploadOk ".$uploadOk;
return $linksave;
}
Check the file_uploads value in PHP.ini and confirm that is something like this
file_uploads = On
Wrap a try/catch in your code. maybe you will find out more about the error.
function UploadFisier($file, $sufix, $target_dir, $maxsize)
{
try {
// your code
} catch (\Exception $e) {
echo $e->getMessage();
die();
}
}
I'm trying to create a script that will send across files from one server to another. My script successfully does that as well as checks if the file has something in it or not. My next step is to check whether the file already exists on the server; if the file already exists it does not send and if it does not exist, it does send.
I've tried a few different things and can't seem to get my head around it. How can I get it to check whether the file already exists or not? Any help would be appreciated!
(I had a look at some similar questions but couldn't find anything specific to my issue.)
require('constants.php');
$files = $sftp->nlist('out/');
foreach($files as $file) {
if(basename((string) $file)) {
if(strpos($file,".") > 1) { //Checks if file
$filesize = $sftp->size('out/'.$file); //gets filesize
if($filesize > 1){
if (file_exists('import/'.$file)){
echo $file.' already exists';
}
else {
$sftp->get('out/'.$file, 'import/'.$file); //Sends file over
//$sftp->delete('out/'.$file); //Deletes file from out folder
}
else {
echo $file. ' is empty.</br>';
}
}
}
}
}
EDIT: To try and get this to work, I wrote the following if statement to see if it was finding the file test.php;
if (file_exists('test.txt')){
echo 'True';
} else {
echo 'False';
}
This returned true (a good start) but as soon as I put this into my code, I just get a 500 Internal Server Error (extremely unhelpful). I cannot turn on errors as it is on a server that multiple people use.
I also tried changing the file_exists line to;
if (file_exists('test.txt'))
in the hopes that would work but still didn't work.
Just to clarify, I'm sending the files from the remote server to my local server.
There is a closing curly brace missing right before the second else keyword.
Please try to use a code editor with proper syntax highlighting and code formatting to spot such mistakes on the fly while you are still editing the PHP file.
The corrected and formatted code:
require('constants.php');
$files = $sftp->nlist('out/');
foreach ($files as $file) {
if (basename((string)$file)) {
if (strpos($file, ".") > 1) { //Checks if file
$filesize = $sftp->size('out/' . $file); //gets filesize
if ($filesize > 1) {
if (file_exists('import/' . $file)) {
echo $file . ' already exists';
} else {
$sftp->get('out/' . $file, 'import/' . $file); //Sends file over
}
} else {
echo $file . ' is empty.</br>';
}
}
}
}
Your code checks the file exist in your local server not in remote server.
if (file_exists('import/'.$file)){
echo $file.' already exists';
}
You need to check in remote server using sftp object like
if($sftp->file_exists('import/'.$file)){
echo $file.' already exists';
}
Edit:
Add clearstatcache() before checking file_exists() function as the results of the function get cached.
Refer: file_exists
EDIT: got it working, but could still use help. Please see below in edit section.
I've searched all over, but this is really confusing me.
I have a form that allows for multiple file uploads. Each file input is named "track[]" so that way it can get associated into an array. When the form is submitted, I set the file information into an array here:
private $tracks = [];
public function setTracks () {
$length = count($_FILES['track']['name']);
for ($i=0; $i < $length; $i++) {
$this->tracks[$i] = ["file_temp" => $_FILES['track']['tmp_name'][$i],
"file_name" => $_FILES['track']['name'][$i],
"file_size" => $_FILES['track']['size'][$i],
"file_type" => $_FILES['track']['type'][$i],
"target_path" => "tracks/" . $_FILES['track']['name'][$i],
"file_error" => $_FILES['track']['error'][$i]];
}
}
This creates the array just fine, but I'm finding it difficult trying to actually save the file by getting the information I need from this array.
This is my rather basic method for uploading images. How can I adapt this for uploading tracks?
public function uploadImg () {
if ($this->file_size > $this->img_max_size) {
echo "The file size is too large. Please compress it and try again.";
}
if (move_uploaded_file($this->file_temp, "../../" . $this->target_path)) {
echo "The image ". $this->file_name . " has been uploaded successfully!";
}
else {
echo $this->file_error . "There was an error uploading the image, please try again!";
}
}
By the way, I think a foreach loop would be better in both my setTracks and my new uploadTracks methods I'm trying to make, rather than the for loop I am currently implementing, but I wasn't able to figure out how to use one in this case.
EDIT: I'm not sure exactly what I did, but I got this to work. Still, if somebody could help me change my for loop to a foreach it would be greatly appreciated. Here's my method that handles the upload:
public function uploadTrack () {
$length = count($this->tracks);
for ($i=0; $i < $length; $i++) {
if ($this->file_size > $this->track_max_size) {
echo "The file size is too large. Please compress it and try again.";
}
else if (move_uploaded_file($this->tracks[$i]["file_temp"], "../../" . $this->tracks[$i]['target_path'])) {
echo "The image ". $this->file_name . " has been uploaded successfully!";
}
else {
echo $this->file_error . "There was an error uploading the image, please try again!";
}
}
}
I have a php page that is supposed to store the uploaded image to my server. When I run this, I get the "Upload successful" message, but the picture has not been uploaded.
What could it be?
update: can people please leave a comment as to why they down vote my question. I'm new here and I dont know why this question got down voted. thanks
<?
if(!empty($_FILES['uploaded_file'])) {
if ($_FILES['uploaded_file']['error'] > 0 )
echo "Error: " . $_FILES['uploaded_file']['error'] . "<br />";
else{
// Add the original filename to target path.
$target_path = 'MemberPics\\user'.$userid.'.jpg' ;
$success = move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path);
if(!$success) {
echo "There was an error uploading the file, please try again!";
}else {
echo "Upload successful, please go back to your home page";
}
}
}
?>
I believe the problem you are running into is that you are saving the image in an incorrect location (An invalid one from the looks of your link syntax).
Either of these should work:
$target_path = 'MemberPics/user'.$userid.'.jpg' ;
or
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], "MemberPics/user" . $_FILES["uploaded_file"]["name"]);
This is the PHP code used for the upload:
$upload = "uploads/";
$upload = $upload . basename($_FILES['bgimage']['name']);
if (move_uploaded_file($_FILES['bgimage']['tmp_name'], $upload)) {
echo "The file has been uploaded successfully.";
} else { echo "Error"; }
When I test the script, it says "The file has been uploaded successfully." but when I check the FTP server, it hasn't really...
Also, if you need to know, here's the HTML codes:
Form tag:
<form name="profilestyle" action="account.php?action=profiletheme" method="post" enctype="multipart/form-data">
Input tag:
<input type="file" name="bgimage" />
Extra Information:
Yes, I remembered the CHMod the uploads directory
Odd, the code looks fine as far as I can see.
Can you use file_exists() to check whether the file exists, but maybe is not visible to your FTP user?
if (move_uploaded_file($_FILES['bgimage']['tmp_name'], $upload)) {
echo "The file '$upload' has been uploaded successfully.";
if (file_exists($upload)) echo "And it exists! It is ".filesize($upload)." bytes big.";
else echo "But it doesn't exist.";
} else { echo "Error"; }
You also need to check $_FILES['bgimage']['error'] to make sure it is equal to UPLOAD_ERR_OK and is not an error code.
Please try the following test code
$upload = "uploads/";
$upload = $upload . basename($_FILES['bgimage']['name']);
sprintf('<pre>Debug: moving file from %s to %s</pre>',
$_FILES['bgimage']['tmp_name'],
$upload
);
if (move_uploaded_file($_FILES['bgimage']['tmp_name'], $upload)) {
echo "The file has been uploaded successfully.";
sprintf('<pre>Debug: realpath=%s, filesize=%d</pre>',
realpath($upload),
filesize($upload)
);
}
else {
echo "Error";
}
and esp. keep an eye on the realpath=xyz output.