我正在使用Magento 2,我需要通过ftp连接读取文件。我可以登录到ftp但我无法读取csv文件。到目前为止我做了什么(我切掉了所有不必要的部分):
aa_AA
如何将csv作为数组读取,因为namespace detail
{
template <typename T, typename F, typename... Ts, size_t... Is>
void apply1(T&& t, F&& f, std::index_sequence<Is...>)
{
const int d[] = { 0, (f(std::get<Is>(t)), 0)... };
}
template <typename T, typename F, size_t... Is>
void apply_by_index1(T&& t, F&& f, std::index_sequence<Is...>)
{
const int d[] = { 0, (f(Is), 0)... };
}
template <size_t I, typename... Ts>
constexpr auto create_element(Ts&&... ts)
{
return std::make_tuple(std::get<I>(std::forward<Ts>(ts))...);
}
template <typename... Ts, size_t... Is>
constexpr auto merge(std::index_sequence<Is...>, Ts&&... ts)
{
return std::make_tuple(create_element<Is>(std::forward<Ts>(ts)...)...);
}
}
template <typename T, typename F>
void apply1(T&& t, F&& f)
{
detail::apply1(std::forward<T>(t), std::forward<F>(f), std::make_index_sequence<std::tuple_size<std::decay_t<T>>::value>());
}
template <typename T, typename F>
void apply_by_index1(T&& t, F&& f)
{
detail::apply_by_index1(std::forward<T>(t), std::forward<F>(f), std::make_index_sequence<std::tuple_size<std::decay_t<T>>::value>());
}
template <typename T, typename... Ts>
constexpr auto merge(T&& t, Ts&&... ts)
{
return detail::merge(std::make_index_sequence<std::tuple_size<std::decay_t<T>>::value>(), std::forward<T>(t), std::forward<Ts>(ts)...);
}
int main() {
const auto t1 = std::make_tuple("Hallo ", "Hallo ", "Hallo ");
const auto t2 = std::make_tuple("World", "Jon", "Peter");
const auto t3 = std::make_tuple("!", "!", "!");
const auto t4 = merge(t1, t2, t3);
apply1(t4, [](const auto& e)
{
std::cout << std::get<0>(e) << std::get<1>(e) << std::get<2>(e) << std::endl;
});
return 0;
}
将返回,但是从远程文件而不是本地?
答案 0 :(得分:0)
您需要解析csv字符串。尝试使用“fgetcsv”功能。请参阅http://media.database/。
答案 1 :(得分:0)
在代码下方,您可以使用读取CSV文件的数组
use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$objectManager1 = Magento\Framework\App\ObjectManager::getInstance();
$directoryList = $objectManager1->get('\Magento\Framework\App\Filesystem\DirectoryList');
$path = $directoryList->getPath('media');
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$myarray = glob("Book1.csv");
usort($myarray, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));
if(count($myarray)){
/*This will create an array of associative arrays with the first row column headers as the keys.*/
$csv_map = array_map('str_getcsv', file($myarray[count($myarray)-1]));
array_walk($csv_map, function(&$a) use ($csv_map) {
$a = array_combine($csv_map[0], $a);
});
array_shift($csv_map); # remove column header
/*End*/
$message = '';
$count = 1;
foreach($csv_map as $data){
your code....
}
希望这会帮助你享受...
答案 2 :(得分:0)
您可以将服务器文件转储到本地。
$filecsv = $this->_ftp->read($remotepathtofile, $destinationFilePath);
其中$ destinationFilePath是本地文件路径。 然后使用该本地文件。