When i upload a excel file, i have used COM() to open and automate converting it to xml.
It works fine, But when i run it, it always shows the message from Microsoft Excel:
A file named ''' already exists in this location. Do you want to replace it?
I can choose between Yes No and Cancel.
normally i would choose Yes. But i dont want users to click on Yes each time.
Can i disable this?
Please inform me if any relevant codes need to be posted.
Thanks
UPDATE Here's part of my code using unlink().
$workbook = $_FILES['file']['tmp_name']
$sheet = "Sheet1";
$ext = substr($workbook, strrpos($workbook, '.') + 1);
$ex = new COM("Excel.sheet") or die("Did not connect");
//Open the workbook that we want to use.
$wkb = $ex->application->Workbooks->Open($workbook) or die("Did not open");
$path = "D:\b2\\test1.xml";
$format = 46;
unlink($path);
$path = "D:\b2\\test1.xml";
//Create a copy of the workbook, so the original workbook will be preserved.
$ex->Application->ActiveWorkbook->SaveAs($path, $format);
Is it the right way to use it? Because it does not seem to work
If you're always going to overwrite, the simplest way is probably just to delete the file first before you get Excel to open it. The PHP function to delete a file is unlink()
I solved it by adding this line.
$ex->application->displayAlerts = 0;
Related
I use a custom CMS in PHP and I need to let admin user to save a global configuration option for his website.
I would like to store this value into a file.
Avoiding to save it into mysql DB, so I don't need to do an extra query on every page load.
What is the best way to store this option from a form into a file?
I need to reload my saved setting so it can be edited with my form
Ini file format simple enough for anyone to understand (read). You might want to paste the file's content into a textarea for simple edition.
For advanced edition, you can use parse_ini_file http://php.net/manual/en/function.parse-ini-file.php .
here is the code to open a php file using php :
$file = "/home/dir/file.php";
$fs = fopen( $file, "a+" ) or die("error when opening the file");
while (!feof($fs)) {
$contents .= fgets($fs, 1024);
}
fclose($fs);
now you can take the $contents and modify it to however you would like and then save it. here is how you can save it :
$fs = fopen( $_POST["file"], "a+" ) or die("error when opening the file");
fwrite($fs, $updatedContents);
fclose();
$updatedContents
is the updated content
how to update a php file's source code via another php file
I have been generating the PDFs and download it. Everything works fine. But whenever the file is output, it simply replaces the previous generated file.
Is there any way to output the file with the different name every time the NEW user downloads it by selecting data from the database?? What changes I can make in Output() method?
I have read output() doc.
$mpdf->Output("PDFs/something.pdf");
// change the path to fit your websites document structure
$fullPath = "PDFs/something.pdf";
You can do:
$filename = 'pdf' . time(). '.pdf';
$mpdf->Output("PDFs/$filename");
You can attach some timestamp/date with filename.
$filename = "PDFs/something-" . time() . ".pdf";
$mpdf->Output($filename);
// change the path to fit your websites document structure
$fullPath = $filename;
With the help of the answer given by Ramesh and some help from Immibis, I have found my way like this. Since the 'proposal_id' will always be unique in my case, so there will be no chance of downloading two pdfs of same id at the same time by different users.
$con = mysqli_connect("localhost","root","","my_db");
$name="Select name FROM table_name WHERE proposal_id= '$id'";
$name_result= mysqli_query($con,$name);
$row_name= mysqli_fetch_array($name_result);
$filename = $row_name['name']. time();
$mpdf->Output("PDFs/$filename.pdf");
$fullPath = "PDFs/$filename.pdf";
I know you can create a temporary file with tmpfile and than write to it, and close it when it is not needed anymore. But the problem I have is that I need the absolute path to the file like this:
"/var/www/html/lolo/myfile.xml"
Can I somehow get the path, even with some other function or trick?
EDIT:
I want to be able to download the file from the database, but without
$fh = fopen("/var/www/html/myfile.xml", 'w') or die("no no");
fwrite($fh, $fileData);
fclose($fh);
because if I do it like this, there is a chance of overlapping, if more people try to download the same file at exactly the same time. Or am I wrong?
EDIT2:
Maybe I can just generate unique(uniqID) filenames like that, and than delete them. Or can this be too consuming for the server if many people are downloading?
There are many ways you can achieve this, here is one
<?php
// Create a temp file in the temporary
// files directory using sys_get_temp_dir()
$temp_file = tempnam(sys_get_temp_dir(), 'MyFileName');
echo $temp_file;
?>
The above example will output something similar to:
/var/tmp/MyFileNameX322.tmp
I know you can create a temporary file with tmpfile
That is a good start, something like this will do:
$fileHandleResource = tmpfile();
Can I somehow get the path, even with some other function or trick?
Yes:
$metaData = stream_get_meta_data($fileHandleResource);
$filepath = $metaData['uri'];
This approach has the benefit of leaving it up to PHP to pick a good place and name for this temporary file, which could end up being a good thing or a bad thing depending on your needs. But it is the simplest way to do this if you don't yet have a specific reason to pick your own directory and filename.
References:
http://us.php.net/manual/en/function.stream-get-meta-data.php
Getting filename (or deleting file) using file handle
This will give you the directory. I guess after that you are on your own.
For newer (not very new lol) versions of PHP (requires php 5.2.1 or higher) #whik's answer is better suited:
<?php
// Create a temp file in the temporary
// files directory using sys_get_temp_dir()
$temp_file = tempnam(sys_get_temp_dir(), 'MyFileName');
echo $temp_file;
?>
The above example will output something similar to: /var/tmp/MyFileNameX322.tmp
old answer
Just in case someone encounters exactly the same problem. I ended up doing
$fh = fopen($filepath, 'w') or die("Can't open file $name for writing temporary stuff.");
fwrite($fh, $fileData);
fclose($fh);
and
unlink($filepath);
at the end when file is not needed anymore.
Before that, I generated filename like that:
$r = rand();
$filepath = "/var/www/html/someDirectory/$name.$r.xml";
I just generated a temporary file, deleted it, and created a folder with the same name
$tempFolder = tempnam(sys_get_temp_dir(), 'MyFileName');
unlink($tempFolder);
mkdir($tempFolder);
I have searched far and wide on this one, but haven't really found a solution.
Got a client that wants music on their site (yea yea, I know..). The flash player grabs the single file called song.mp3 and plays it.
Well, I am trying to get functionality as to be able to have the client upload their own new song if they ever want to change it.
So basically, the script needs to allow them to upload the file, THEN overwrite the old file with the new one. Basically, making sure the filename of song.mp3 stays intact.
I am thinking I will need to use PHP to
1) upload the file
2) delete the original song.mp3
3) rename the new file upload to song.mp3
Does that seem right? Or is there a simpler way of doing this? Thanks in advance!
EDIT: I impimented UPLOADIFY and am able to use
'onAllComplete' : function(event,data) {
alert(data.filesUploaded + ' files uploaded successfully!');
}
I am just not sure how to point THAT to a PHP file....
'onAllComplete' : function() {
'aphpfile.php'
}
???? lol
a standard form will suffice for the upload just remember to include the mime in the form. then you can use $_FILES[''] to reference the file.
then you can check for the filename provided and see if it exists in the file system using file_exists() check for the file name OR if you don't need to keep the old file, you can use perform the file move and overwrite the old one with the new from the temporary directory
<?PHP
// this assumes that the upload form calls the form file field "myupload"
$name = $_FILES['myupload']['name'];
$type = $_FILES['myupload']['type'];
$size = $_FILES['myupload']['size'];
$tmp = $_FILES['myupload']['tmp_name'];
$error = $_FILES['myupload']['error'];
$savepath = '/yourserverpath/';
$filelocation = $svaepath.$name.".".$type;
// This won't upload if there was an error or if the file exists, hence the check
if (!file_exists($filelocation) && $error == 0) {
// echo "The file $filename exists";
// This will overwrite even if the file exists
move_uploaded_file($tmp, $filelocation);
}
// OR just leave out the "file_exists()" and check for the error,
// an if statement either way
?>
try this piece of code for upload and replace file
if(file_exists($newfilename)){
unlink($newfilename);
}
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newfilename);
I am completely new to PHP so forgive me if this question seems very rudimentry. And thank you in advance.
I need to include a jpg that is generated from a webcam on another page. However I need to include only the latest jpg file. Unfortunately the webcam creates a unique filename for each jpg. How can I use include or another function to only include the latest image file?
(Typically the filename is something like this 2011011011231101.jpg where it stands for year_month_date_timestamp).
Easy way is to get the latest image with the help of the below code
$path = "/path/to/my/dir";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
}
// now $latest_filename contains the filename of the newest file
give the source of latest image to <img> tag
Since the images are named via pattern which relates to the date, you should be able to just use:
$imgs = glob('C:\images\*.jpg');
rsort($imgs);
$newestImage = $imgs[0];
This is fairly straightforward, since your file names are in order.
The first thing you need is a list of files in the directory. The readdir (doc) function is what you are looking for. Example script that uses it: http://www.liamdelahunty.com/tips/php_list_a_directory.php
Once you have that, use substr() (doc) to chop off the file name extensions.
You're left with an array of numbers, essentially. From here, do a sort (doc) and specify the SORT_NUMERIC flag. Grab the number on the end, stick a .jpg back on it, and you have the last file.
Alternate Solution: Read the timestamps of files to get the last one. This would generally be a better answer, but perhaps not in your situation if you plan to edit any of the files.
I guess you will have to know a way to determine what the latest image file is called. Maybe you can make a textfile or something where every time a new image is created the webcam writes the latest filename in the text file (so the only text in the text file is the file name of the latest image file if it makes any sense). Of course you will have to have access to the script that generates the php file.
addition to #ken 's post, it's probably sorting alphabetically instead of numerically. perhaps you could try:
$imgs = glob('C:\images\*.jpg');
rsort($imgs, SORT_NUMERIC);
$newestImage = $imgs[0];