-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DomCrawler] FileFormField::setValue ignores file extension #4674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is the same behavior as PHP. The |
@fabpot, @everzet: The problem is that DOMCrawler simulates HTTP request only on server side. But if we want create request on client side (for example for Goutte), DOMCrawler doesn't allowed to create new file with random name, but it have to use original file. I suggest this changes: <?php
/**
* Upload the file of the field.
*
* @param string $value The value of the field
*
* @api
*/
public function upload($value)
{
if (null !== $value && is_readable($value)) {
$error = UPLOAD_ERR_OK;
$size = filesize($value);
$name = basename($value);
// copy to a tmp location
$tmp = tempnam(sys_get_temp_dir(), 'upload');
unlink($tmp);
copy($value, $tmp);
$value = $tmp;
} else {
$error = UPLOAD_ERR_NO_FILE;
$size = 0;
$name = '';
$value = '';
}
$this->value = array('name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size);
}
/**
* Sets the value of the field.
*
* @param string $value The value of the field
*/
public function setValue($value, $raw = false)
{
if ($raw) {
parent::setValue($value);
} else {
$this->upload($value);
}
} https://github.com/fabpot/Goutte/blob/master/Goutte/Client.php#L140 <?php
protected function addPostFiles($request, array $files, $arrayName = '')
{
if (!$request instanceof EntityEnclosingRequestInterface) {
return;
}
foreach ($files as $name => $info) {
if (!empty($arrayName)) {
$name = $arrayName . '[' . $name . ']';
}
if (isset($info['tmp_name']) && '' !== $info['tmp_name']) {
$request->addPostFile($name, $info['tmp_name']);
} elseif (is_array($info)) {
$this->addPostFiles($request, $info, $name);
+ } else {
+ $request->addPostFile($name, $info);
+ }
}
} https://github.com/Behat/MinkGoutteDriver/blob/master/src/Behat/Mink/Driver/GoutteDriver.php#L71 <?php
/**
* Attaches file path to file field located by it's XPath query.
*
* @param string $xpath
* @param string $path
*/
public function attachFile($xpath, $path)
{
$this->getFormField($xpath)->setValue($path, true);
} What do you think? Can I prepare pull request? |
This PR was merged into the master branch. Commits ------- c902966 [DomCrawler] Added ability to set file as raw path to file field Discussion ---------- [2.2][DomCrawler] Added ability to set file as raw path to file field Bug fix: no Feature addition: yes Backwards compatibility break: no Symfony2 tests pass: yes License of the code: MIT For description see #4674 (#4674 (comment)) Related PRs: Behat/MinkBrowserKitDriver#1 https://github.com/Behat/MinkGoutteDriver/pull/7 FriendsOfPHP/Goutte#77 --------------------------------------------------------------------------- by stof at 2012-10-13T21:53:27Z @fabpot anything missing here ?
This PR was merged into the master branch. Commits ------- c902966 [DomCrawler] Added ability to set file as raw path to file field Discussion ---------- [2.2][DomCrawler] Added ability to set file as raw path to file field Bug fix: no Feature addition: yes Backwards compatibility break: no Symfony2 tests pass: yes License of the code: MIT For description see #4674 (symfony/symfony#4674 (comment)) Related PRs: Behat/MinkBrowserKitDriver#1 https://github.com/Behat/MinkGoutteDriver/pull/7 FriendsOfPHP/Goutte#77 --------------------------------------------------------------------------- by stof at 2012-10-13T21:53:27Z @fabpot anything missing here ?
This function creates unique temp name, so it doesn't preserve name and extension of file passed in $value param. It's a problem if using DomCrawler to simulate file uploads (Behat + Mink). Can you fix it or is there any solution now? Thanks
The text was updated successfully, but these errors were encountered: