After some problems i could finally upload files with google app engine to my bucket, but now the name of the uploaded file is unreadable, like a temp name, encrypted or something, like this:
L2FwcGhvc3RpbmdfcHJvZC9ibG9icy9BRW5CMlVvQWFjdkRYbWhtY1dPRGc2ZjlkVzRUU0lOV0FFMThWZnAxbUl1MzFVUndLSWdYVTBvdHhyYXl4UWdNOElXWklvX2hkQjdfaHYxbWNvc0dlSEtSQ184enJCU3M4QS5PTW5KWVBiTWdWZTJGdmQ4
How can i give the name that i want to an uploaded file???
I'm using the same code as the one given in the google developers documentation, like this:
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;
$options = [ 'gs_bucket_name' => 'my_bucket' ];
$upload_url = CloudStorageTools::createUploadUrl('/upload_handler.php', $options);
<form action="<?php echo $upload_url?>" enctype="multipart/form-data" method="post">
Files to upload: <br>
<input type="file" name="uploaded_files" size="40">
<input type="submit" value="Send">
</form>
Does anybody know if Amazon's S3 is easier and more standard friendly? Also if it's posible to comunicate with GAP, I think i read that it is posible, but now i'm not sure about anything, too late and my brain is burnt :(
Thanks in advance!!
I made it work, it was easy, i just missunderstood something...
It must be done in upload_handler.php, is obvious but...xD. And as Mars says with move_uploaded_file, or with rename. Something like this (maybe is usefull for someone):
$gs_tmpName = $_FILES["myfile"]['tmp_name'];
$ext = strrchr($gs_name, ".");
$desired_name = "<desired_name>$ext";
$desired_name = mb_convert_encoding($desired_name, "UTF-8", "AUTO");
$desired_name = urlencode($desired_name);
$options = array('gs'=>array('acl'=>'public-read','Content-Type' => $_FILES['myfile']['type']));
$ctx = stream_context_create($options);
rename($gs_tmpName, "gs://<bucket_name>/".$desired_name, $ctx);
$publicUrl = CloudStorageTools::getPublicUrl('gs://<bucket_name>/'.$desired_name , true);
Related
I have code in php that create file with name that user input like below
html
<form method="post">
<input type="text" name="file">
<input type="submit">
</form>
php
$file = $_POST['file'].'.php';
$f = fopen($file, 'w');
fclose($f);
the problem appear when user use non-english chars
example user input = اللغة العربيه
resault = ظ…ط±ط§ظ‡ظ‚ظˆ-ط§ظ„طھط§ظٹطھظ†ط.php
but it should be = اللغة العربيه.php
Trying to create files with non-english filenames can be quite hard.
Easiest solution is to not use such filenames at all, but encode all filenames e.g. with urlencode:
$filename = $_POST['file'].'.php';
$encoded_filename = urlencode($filename);
$f = fopen($encoded_filename, 'w');
fclose($f);
This will not affect English characters and will allow creating filenames using any language.
Note that each filesystem will have some limits on how long filesnames can be, so if $encoded_filename becomes too long this will not work.
Im new to service cloud and have been tasked with figuring out a way to save file attachments to custom objects. I believe what I have thus far accomplishes just that as I can see the new records being created in my object. My next step is to figure out a way to find uploaded files given an ID and display a clickable link that will open in a new tab to display the uploaded file's contents. (similar to the stock FileListDisplay widget). According to the Connect PHP API documentation, no ROQL query or fetch() returns any file attachment data. How would I go about displaying uploaded files?
Code so far (shortened for brevity)
View:
<form action="https://test.rightnowdemo.com/app/Upload2/uploadcontroller2" method="post" enctype="multipart/form-data" />
<div>
<h3>Select a .txt file only:</h3><br />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<label for="userfile">Select a file:</label>
<input type="file" class="form-control" name="userfile" /><br>
<input type="submit" class="nybeButton" value="Send" />
</div>
</form>
controller:
function store_attachment($FileName, $FileLocation, $FileType)
{
$MyDocument = new RightNow\Connect\v1_1\UPLOAD\Documents();
$MyDocument->FileAttachments = new RightNow\Connect\v1_1\FileAttachmentArray();
$MyDocument->UserName = "Test user";
$fattach = new RightNow\Connect\v1_1\FileAttachment();
$fattach->ContentType = $FileType;
$file = $FileLocation;
$fattach->setFile($file);
$fattach->FileName = $FileName;
$MyDocument->FileAttachments[] = $fattach;
//\RightNow\Libraries\AbuseDetection::check();
$MyDocument->save();
}
$FileName = $_FILES["userfile"]["name"];
$FileLocation = get_cfg_var('upload_tmp_dir') . "/" . $_FILES["userfile"]["name"];
$FileType = $_FILES["userfile"]["type"];
store_attachment($FileName, $FileLocation, $FileType);
A similar question was asked on the OSvC communities lately. Generally, there's no way to get the file data through Connect for PHP; you can only set through Connect (we used to have some scripts to get around this with PHP, but I believe that Prod Dev has closed all PHP methods to access the file servers). You must use the SOAP API to retrieve the files through your code.
However, if you just want to display a link on Customer Portal to the file, then you can follow the following format, as per Arun's answer in the communities:
Download File
Hello I am having trouble getting imagepng to not only create a image on my server but name it a specific name according to a POST input from a form on another page.
It will display the finished image on screen just fine but depending on which method I try (tried many), it either creates a file name ".png" or does not create one at all.
Here is the "form.html" the info is pulling from:
<form action="test.php" method="post"><BR><BR>
Type Forum Name:
<input type="text" name="name" value=""/><BR><BR>
<input type="submit" name="Submit1"><BR><BR>
</form><BR>
And here is "test.php":
<?php
$name = $_POST["name"];
$input1 = $_POST["fsbg"];
$input2 = $_POST["rank"];
header ("Content-type: image/png");
$background = imagecreatefrompng($fsbg);
$pkt = imagecreatefromgif($rank);
imagecopymerge($background,$pkt,260,136,0,0,55,55,100);
imagepng($background);
$save = strtolower($name) .".png";
imagepng($background, $save);
imagedestroy($background);
imagedestroy($pkt);
header('location:link.php');
?>
I have set folder/sub folder permissions to 777, tried saving in the same path as the file, as well as saving in a sub folder called "pvt".
Any help/Advice would be greatly appreciated, Thanks!
You do not have a "name" field in the form. You have a unnamed field whose value is "name":
<input type="text" value="name"/><BR><BR>
Needs to be:
<input type="text" name="name" value="name"/><BR><BR>
Also, when you do this, sanitize the input. What if I sent you name="../../etc/passwd" or something like that? (of course this example won't work. But still.).
At the very least use basename to strip all path components from the name, and realpath to ensure you're writing where you should.
I finally figured this out.
Turns out even though I was changing the Permissions in my FTP, I still needed to add this in to get it to save a file: chmod($name,0755);
Completed code looks like:
$background = imagecreatefrompng($fsbg);
$pkt = imagecreatefromgif($rank);
//$pkt2 = imagecreatefromgif($rank);
imagecopymerge($background,$pkt,260,136,0,0,55,55,100);
//imagecopymerge($background,$pkt2,290,136,0,0,55,55,100);
header( "Content-type: image/png" );
imagepng($background);
$name = $filename.".png";
chmod($name,0755);
imagepng($background, $name);
imagedestroy($background);
imagedestroy($pkt);
Thanks to all who tooks the time to reply.
How do i update a variable in a php file and save it to disk? I'm trying to make a small CMS, in one file, no database, and i'd like to be able to update the config variables, I've made a settings page and when i update i'd like the variables to updated in the php-file.
Below will update the variable, but of course not save it to the php-file, so how do i do that?
<?php
$USER = "user";
if (isset($_POST["s"]) ) { $USER = $_POST['USER']; }
?>
<html><body>
Current User: <?php echo $USER; ?>
<form method="post" action="<?php echo $_SERVER["SCRIPT_NAME"]; ?>">
New User<input type="text" name="USER" value="<?php echo $USER; ?>"/>
<input type="submit" class="button" name="s" value="Update" />
</form>
</body></html>
I don't know if i'm missing the obvious?
I was thinking of using something like this:
$newcms = file_get_contents(index.php)
str_replace(old_value, new_value, $newcms)
file_puts_contents(index.php, $newcms)
But it doesn't seem like the right solution...
The easiest way is to serialize them to disk and then load them so you might have something like this:
<?php
$configPath = 'path/to/config.file';
$config = array(
'user' => 'user',
);
if(file_exists($configPath)) {
$configData = file_get_contents($configPath);
$config = array_merge($defaults, unserialize($configData));
}
if(isset($_POST['s']) {
// PLEASE SANTIZE USER INPUT
$config['user'] = $_POST['USER'];
// save it to disk
// Youll want to add some error detection messagin if you cant save
file_put_contents($configPath, serialize($config));
}
?>
<html><body>
Current User: <?php echo $config['user'; ?>
<form method="post" action="<?php echo $_SERVER["SCRIPT_NAME"]; ?>">
New User<input type="text" name="USER" value="<?php echo $config['user']; ?>"/>
<input type="submit" class="button" name="s" value="Update" />
</form>
</body></html>
This approach uses PHP's native serialization format which isnt very human readable. If you want to be able to manually update configuration or more easily inspect it you might want to use a different format like JSON, YAML, or XML. JSON will probably be nearly as fast and really easy to work with by using json_encode/json_decode instead of serialize/unserialize. XML will be slower and more cumbersome. YAML is pretty easy to work with too but youll need an external library like sfYaml.
Additionally i wouldnt just do this at the top of a script, id probably make a class of it or a series of functions at the least.
As a better approach, you can have a separate file just for the settings and include that file in the PHP file. Then you can change and save its values as you want instead of modifying the PHP file itself.
for example, you have YOURFILE.php, and inside it you have $targetvariable='hi jenny';
if you want to change that variable, then use this:
<?php
$fl='YOURFILE.php';
/*read operation ->*/ $tmp = fopen($fl, "r"); $content=fread($tmp,filesize($fl)); fclose($tmp);
// here goes your update
$content = preg_replace('/\$targetvariable=\"(.*?)\";/', '$targetvariable="hi Greg";', $content);
/*write operation ->*/ $tmp =fopen($fl, "w"); fwrite($tmp, $content); fclose($tmp);
?>
My question is, how and will <input type='file' name='file[]'> multiple of theese work in php?
Thanks
If you loop through them as an array, it'll work just the same:
foreach($_FILES['file'] AS $key=>$file) {
$filename = $file['tmp_name'];
$size = $file['size'];
$newfile = $_SERVER['DOCUMENT_ROOT'] . "/uploads/" . date("Ymd_his") . "_" . $filename;
move_uploaded_file($filename, $newfile);
}
And it'll loop through each upload and process as such.
Just be sure that you have something that changes between each (I added a timestamp) - otherwise you'll only end up with one file.
You were right with the inputs as
<input type="file" name="file[]" />
You can have as many of those as you'd like
Yes they'll work. $_FILES will be an array of uploaded files in PHP.
Yes this will of course work. Just have a look at the $_FILES superglobal array. All your uploaded files and their meta data will be stored there.
According to the W3C's HTML5 draft, you should add the multiple attribute :
<input type="file" name="file[]" multiple="multiple">
Some browser (like Internet Explorer (even 9)) don't support multiple attribute but major other ones does.
Then, you can get all the file by looping into the $_FILES superglobal like that :
<?php
foreach ($_FILES['file']['error'] as $k => $error) {
if (!$error) {
move_uploaded_file($_FILES['file']['tmp_name'][$k], $your_dir.'/'.$_FILES['file']['name'][$k]);
}
}
?>
As far as I am able to tell, if you want to do multiple file uploads from a html form, you need to have multiple file input boxes.
<input type='file' name='file1'>
<input type='file' name='file2'>
<input type='file' name='file3'>
Unless you have some type of java, javascript, flex, or similar multiple file upload framework to do the work for you.
Once you get it uploaded, the PHP script would look like:
<?
foreach($_FILES as $file){
move_uploaded_file($file[tmp_name],$target.$file[name]);
}
?>